4.14 Displaying a Refresh Control for Table Views

本文详细解释了列表刷新的工作原理及实现方式,包括创建刷新控制组件、实现刷新方法和注意事项。特别关注了在不同场景下(如UITableViewController与UIViewController)实现列表刷新的不同之处。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

列表的刷新
列表一般都是怎么刷新的呢,一般都是往下狠狠拽一下是不是,然后就出来一个转转转的表示正在刷新,刷新完恢复。这是怎么实现的呢?
很简单,比你想象的要简单

首先声明创建下这个装转转的东西(书上源码)
self.refreshControl = [[UIRefreshControl alloc] init];
self.refreshControl = self.refreshControl;
[self.refreshControl addTarget:self
action:@selector(handleRefresh:)
forControlEvents:UIControlEventValueChanged];

实现下刷新方法(书上源码)
- (void) handleRefresh:(id)paramSender{
/* Put a bit of delay between when the refresh control is released
and when we actually do the refreshing to make the UI look a bit
smoother than just doing the update without the animation */
int64_t delayInSeconds = 1.0f;
dispatch_time_t popTime =
dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
/* Add the current date to the list of dates that we have
so that when the table view is refreshed, a new item will appear
on the screen so that the user will see the difference between
the before and the after of the refresh */
[self.times addObject:[NSDate date]];//修改数据源
[self.refreshControl endRefreshing];
[self.tableView reloadData];
});
}

不过很悲催的是按上面的做法,我在UITableViewController下成功实现了,但在UIViewController下手动加上TableView却没实现,不知道要如何修改。who help me

 

下一章storyBoard

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值