首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何判断UITableView何时完成了ReloadData?

如何判断UITableView何时完成了ReloadData?
EN

Stack Overflow用户
提问于 2013-04-18 06:41:34
回答 10查看 137.7K关注 0票数 206

在执行完[self.tableView reloadData]之后,我正在尝试滚动到UITableView的底部

我原本有

代码语言:javascript
复制
 [self.tableView reloadData]
 NSIndexPath* indexPath = [NSIndexPath indexPathForRow: ([self.tableView numberOfRowsInSection:([self.tableView numberOfSections]-1)]-1) inSection: ([self.tableView numberOfSections]-1)];

[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

但是后来我读到reloadData是异步的,所以滚动不会发生,因为self.tableView[self.tableView numberOfSections][self.tableView numberOfRowsinSection都是0。

谢谢!

奇怪的是我使用的是:

代码语言:javascript
复制
[self.tableView reloadData];
NSLog(@"Number of Sections %d", [self.tableView numberOfSections]);
NSLog(@"Number of Rows %d", [self.tableView numberOfRowsInSection:([self.tableView numberOfSections]-1)]-1);

在控制台中,它返回Sections = 1,Row = -1;

当我在cellForRowAtIndexPath中执行完全相同的NSLogs时,我得到Sections =1和Row = 8;(8是正确的)

EN

回答 10

Stack Overflow用户

发布于 2017-04-02 05:29:49

从Xcode8.2.1、iOS 10和swift 3开始,

您可以使用CATransaction块轻松确定tableView.reloadData()的结尾:

代码语言:javascript
复制
CATransaction.begin()
CATransaction.setCompletionBlock({
    print("reload completed")
    //Your completion code here
})
print("reloading")
tableView.reloadData()
CATransaction.commit()

上面的代码还用于确定UICollectionView的reloadData()和UIPickerView的reloadAllComponents()的结尾。

票数 60
EN

Stack Overflow用户

发布于 2016-10-19 02:17:04

我使用这个技巧,我很确定我已经把它贴到了这个问题的副本上:

代码语言:javascript
复制
-(void)tableViewDidLoadRows:(UITableView *)tableView{
    // do something after loading, e.g. select a cell.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // trick to detect when table view has finished loading.
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tableViewDidLoadRows:) object:tableView];
    [self performSelector:@selector(tableViewDidLoadRows:) withObject:tableView afterDelay:0];

    // specific to your controller
    return self.objects.count;
}
票数 5
EN

Stack Overflow用户

发布于 2015-06-02 21:20:12

实际上这个解决了我的问题:

代码语言:javascript
复制
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

NSSet *visibleSections = [NSSet setWithArray:[[tableView indexPathsForVisibleRows] valueForKey:@"section"]];
if (visibleSections) {
    // hide the activityIndicator/Loader
}}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16071503

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档