How can properties be updated in custom Section Header that was implemented via the storyboard?

40 views Asked by At

I have a custom section header view class implemented using the storyboard. I can set the various labels and views as required when the view is loaded within the viewForHeaderInSection method.

static NSString *CellIdentifier = @"HeaderCell";
HourlyCustomTableSectionHeader* headerCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

NSString *sectionHeaderTitle = [[NSString alloc] init];

if(section==0){
    sectionHeaderTitle = @"TODAY";
headerCell.CustomHeaderTitle.text = sectionHeaderTitle;
}

However, I want to update some of these from another method that is run each time a new cell is viewable.

Basically- I'd like to set the background colour of the section header to match the uppermost cell and be updated as the user scrolls.

I having a problem trying to reference the properties I want to update. For simplicities sake I am trying to update the custom text label first.

HourlyCustomTableSectionHeader* customSectionHeadObj = [[HourlyCustomTableSectionHeader alloc] init];
customSectionHeadObj.CustomHeaderTitle.text =  @"new title";

Xcode seems to be fine with the path (no errors shown) and I can see the method is being called fine each time the table view is updated and a new cell is in view but the label in the section header is not being updated.

Am I going about this the right way? Or perhaps I need to refresh the section header to make the update visible?

2

There are 2 answers

1
mityaika07 On

I think, it good idea to for it on:

- (void)scrollViewDidScroll:(UIScrollView)scrollView
{

}

and, you should check, in which queue/thread are you call label update, it should be called only from main thread

1
Taylermade On

After much investigation and experimenting I have concluded that it is not possible to update the properties of a custom section header that has been added via the story board. Though I would be happy for anyone to post evidence otherwise.

As a solution / work around the method I implemented was creating the headers programmatically as UIViews in the table view viewForHeaderInSection. These UIViews (and their subviews) can be updated without issue - whenever required (such as scrollViewDidScroll as mityaika07 suggested) - and without requiring reloading the table or other complications.

I've added a test case to help illustrate the issue