Skip to content

Instantly share code, notes, and snippets.

@raunakp
Last active December 29, 2015 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raunakp/7638557 to your computer and use it in GitHub Desktop.
Save raunakp/7638557 to your computer and use it in GitHub Desktop.
Dynamically change UIView for UITableView section header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 13)];
label.textColor = [UIColor blueColor];
label.text = [NSString stringWithFormat:@"%d", section];
label.tag = 100 + section;
return label;
}
/*
// This is not getting called
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
UILabel *label = (UILabel *)view;
label.textColor = [UIColor redColor];
}
*/
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSArray *visible = [self.tableView indexPathsForVisibleRows];
[visible enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
UIView *view = [self.tableView viewWithTag:100+ ((NSIndexPath *)obj).section];
if ([view respondsToSelector:@selector(setTextColor:)])
{
[(UILabel *)view setTextColor:[UIColor blueColor]];
}
}];
NSIndexPath *indexPath = (NSIndexPath*)[visible objectAtIndex:0];
NSLog(@"indexPath section: %d row: %d", indexPath.section, indexPath.section);
UIView *view = [self.tableView viewWithTag:100+indexPath.section];
if ([view respondsToSelector:@selector(setTextColor:)])
{
[(UILabel *)view setTextColor:[UIColor redColor]];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment