UITableView实现下拉刷新

UIRefreshConteol类是实现下拉刷新的控件,并且UITableView有refreshControl的属性。

定义:

UIRefreshControl *control = [[UIRefresh alloc]init];
//下拉刷新时显示的文字
control.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];

需要注意的问题是用户下拉进行刷新时需要程序手动进行关闭,因此需要监控control的下拉事件,然后进行内容的刷新和刷新视图的关闭处理。

[control addTarget:self action:@selector(refreshTableView) forControlEvents:UIControlEventValueChanged];
//延迟2s执行,否则正在刷新时新cell已经显示
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //刷新后新增cell
        [self.data insertObject:@"new iPhone" atIndex:0];
        
        [self.tableView reloadData];
        
        //判断是否正在下拉刷新状态,若是 停止刷新
        if ([self.tableView.refreshControl isRefreshing]) {
            
            [self.tableView.refreshControl endRefreshing];
        }
    });

最后添加control到UITableView

self.tableview.refreshControl = control;

demo下载:

https://download.csdn.net/download/judgejames/12101595

发布了85 篇原创文章 · 获赞 28 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/judgejames/article/details/103972981