Last active
December 29, 2015 07:49
-
-
Save raunakp/7638557 to your computer and use it in GitHub Desktop.
Dynamically change UIView for UITableView section header
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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