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<NSIndexPath *> *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];
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,424评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,159评论 19 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,726评论 4 61
  • 火车站前 刚刚看到一个人吃着好吃的过去了,看的我直流口水,说实话我已经前胸贴后背了。嗯,来上三块钱的烙饼再来一碗紫...
    伟大的自由主义者阅读 1,499评论 0 0
  • 久违的琴键声,拨动着我的心玄!我把幸运叫努力! 曾经的一次擦肩而过,让我对它一见钟情!很多时候我想象着自己和以怎样...
    书痴姑娘阅读 1,733评论 2 2

友情链接更多精彩内容