UITableView时时刷新可视的headerView

1.思路

1.返回自定义的headerView
2.定时器每秒刷新
3.获得可视的headerView的IndexPath数组
4.遍历IndexPath数组
5.通过IndexPath获得指定的headerView
6.修改headerView的控件内容

2.自定义的headerView

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HeaderView *headerView = [[HeaderView alloc] init];
    return headerView;
}


@interface HeaderView : UITableViewHeaderFooterView
@property(nonatomic,weak,readonly) UILabel  *label;
@end

3.定时器每秒刷新

self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTableView) userInfo:nil repeats:YES];
//异步刷新
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

4.headerView处理

- (void)updateTableView{
    NSArray *indexPathArr = [self.tableView indexPathsForVisibleRows];
    self.index++;
    for (NSIndexPath *indexPath in indexPathArr) {
       HeaderView *headerView = (HeaderView *)[self.tableView headerViewForSection:indexPath.section];
        headerView.label.text = [NSString stringWithFormat:@"%d",self.index];
    }
}

你可能感兴趣的:(UITableView时时刷新可视的headerView)