在执行完[self.tableView reloadData]
之后,我正在尝试滚动到UITableView的底部
我原本有
[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。
谢谢!
奇怪的是我使用的是:
[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是正确的)
发布于 2017-04-02 05:29:49
从Xcode8.2.1、iOS 10和swift 3开始,
您可以使用CATransaction块轻松确定tableView.reloadData()
的结尾:
CATransaction.begin()
CATransaction.setCompletionBlock({
print("reload completed")
//Your completion code here
})
print("reloading")
tableView.reloadData()
CATransaction.commit()
上面的代码还用于确定UICollectionView的reloadData()和UIPickerView的reloadAllComponents()的结尾。
发布于 2016-10-19 02:17:04
我使用这个技巧,我很确定我已经把它贴到了这个问题的副本上:
-(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;
}
发布于 2015-06-02 21:20:12
实际上这个解决了我的问题:
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSSet *visibleSections = [NSSet setWithArray:[[tableView indexPathsForVisibleRows] valueForKey:@"section"]];
if (visibleSections) {
// hide the activityIndicator/Loader
}}
https://stackoverflow.com/questions/16071503
复制相似问题