我试图滚动到UITableView的底部后,它完成执行[自我。tableView reloadData]。
我原本有
[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 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做完全相同的nslog时,我得到Sections = 1和Row = 8;(8是正确的)
创建CATransaction的可重用扩展:
public extension CATransaction {
static func perform(method: () -> Void, completion: @escaping () -> Void) {
begin()
setCompletionBlock {
completion()
}
method()
commit()
}
}
现在创建一个UITableView的扩展,使用CATransaction的扩展方法:
public extension UITableView {
func reloadData(completion: @escaping (() -> Void)) {
CATransaction.perform(method: {
reloadData()
}, completion: completion)
}
}
用法:
tableView.reloadData(completion: {
//Do the stuff
})
创建CATransaction的可重用扩展:
public extension CATransaction {
static func perform(method: () -> Void, completion: @escaping () -> Void) {
begin()
setCompletionBlock {
completion()
}
method()
commit()
}
}
现在创建一个UITableView的扩展,使用CATransaction的扩展方法:
public extension UITableView {
func reloadData(completion: @escaping (() -> Void)) {
CATransaction.perform(method: {
reloadData()
}, completion: completion)
}
}
用法:
tableView.reloadData(completion: {
//Do the stuff
})