我试图滚动到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是正确的)
上面的dispatch_async(dispatch_get_main_queue())方法不能保证有效。我看到了它的非确定性行为,其中有时系统在完成块之前完成了layoutSubviews和单元格渲染,有时在完成块之后。
这里有一个解决方案,100%适用于我,在iOS 10上。它需要实例化UITableView或UICollectionView作为自定义子类的能力。这是UICollectionView的解决方案,但它对UITableView是完全一样的:
CustomCollectionView.h:
#import <UIKit/UIKit.h>
@interface CustomCollectionView: UICollectionView
- (void)reloadDataWithCompletion:(void (^)(void))completionBlock;
@end
CustomCollectionView.m:
#import "CustomCollectionView.h"
@interface CustomCollectionView ()
@property (nonatomic, copy) void (^reloadDataCompletionBlock)(void);
@end
@implementation CustomCollectionView
- (void)reloadDataWithCompletion:(void (^)(void))completionBlock
{
self.reloadDataCompletionBlock = completionBlock;
[self reloadData];
}
- (void)layoutSubviews
{
[super layoutSubviews];
if (self.reloadDataCompletionBlock) {
self.reloadDataCompletionBlock();
self.reloadDataCompletionBlock = nil;
}
}
@end
使用示例:
[self.collectionView reloadDataWithCompletion:^{
// reloadData is guaranteed to have completed
}];
请看这个答案的Swift版本
只是提供另一种方法,基于补全是发送到cellForRow的“最后可见”单元格的思想。
// Will be set when reload is called
var lastIndexPathToDisplay: IndexPath?
typealias ReloadCompletion = ()->Void
var reloadCompletion: ReloadCompletion?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Setup cell
if indexPath == self.lastIndexPathToDisplay {
self.lastIndexPathToDisplay = nil
self.reloadCompletion?()
self.reloadCompletion = nil
}
// Return cell
...
func reloadData(completion: @escaping ReloadCompletion) {
self.reloadCompletion = completion
self.mainTable.reloadData()
self.lastIndexPathToDisplay = self.mainTable.indexPathsForVisibleRows?.last
}
一个可能的问题是:如果reloadData()在lastIndexPathToDisplay被设置之前已经完成,“最后可见”单元格将在lastIndexPathToDisplay被设置之前显示,补全将不会被调用(并且将处于“等待”状态):
self.mainTable.reloadData()
// cellForRowAt could be finished here, before setting `lastIndexPathToDisplay`
self.lastIndexPathToDisplay = self.mainTable.indexPathsForVisibleRows?.last
如果反转,可能会在reloadData()之前通过滚动触发补全。
self.lastIndexPathToDisplay = self.mainTable.indexPathsForVisibleRows?.last
// cellForRowAt could trigger the completion by scrolling here since we arm 'lastIndexPathToDisplay' before 'reloadData()'
self.mainTable.reloadData()
上面的dispatch_async(dispatch_get_main_queue())方法不能保证有效。我看到了它的非确定性行为,其中有时系统在完成块之前完成了layoutSubviews和单元格渲染,有时在完成块之后。
这里有一个解决方案,100%适用于我,在iOS 10上。它需要实例化UITableView或UICollectionView作为自定义子类的能力。这是UICollectionView的解决方案,但它对UITableView是完全一样的:
CustomCollectionView.h:
#import <UIKit/UIKit.h>
@interface CustomCollectionView: UICollectionView
- (void)reloadDataWithCompletion:(void (^)(void))completionBlock;
@end
CustomCollectionView.m:
#import "CustomCollectionView.h"
@interface CustomCollectionView ()
@property (nonatomic, copy) void (^reloadDataCompletionBlock)(void);
@end
@implementation CustomCollectionView
- (void)reloadDataWithCompletion:(void (^)(void))completionBlock
{
self.reloadDataCompletionBlock = completionBlock;
[self reloadData];
}
- (void)layoutSubviews
{
[super layoutSubviews];
if (self.reloadDataCompletionBlock) {
self.reloadDataCompletionBlock();
self.reloadDataCompletionBlock = nil;
}
}
@end
使用示例:
[self.collectionView reloadDataWithCompletion:^{
// reloadData is guaranteed to have completed
}];
请看这个答案的Swift版本
当[tableView reloadData]返回时,tableView背后的内部数据结构已经被更新。因此,当方法完成时,您可以安全地滚动到底部。我在自己的应用程序中验证了这一点。rob mayoff的答案被广泛接受,尽管术语也令人困惑,但他在最近的更新中承认了这一点。
如果你的tableView没有滚动到底部,你可能在你没有发布的其他代码中有问题。也许在滚动完成后,您正在更改数据,并且没有重新加载和/或滚动到底部?
添加如下的日志记录,以验证reloadData之后的表数据是否正确。我在一个示例应用程序中有以下代码,它可以完美地工作。
// Change the data source
NSLog(@"Before reload / sections = %d, last row = %d",
[self.tableView numberOfSections],
[self.tableView numberOfRowsInSection:[self.tableView numberOfSections]-1]);
[self.tableView reloadData];
NSLog(@"After reload / sections = %d, last row = %d",
[self.tableView numberOfSections],
[self.tableView numberOfRowsInSection:[self.tableView numberOfSections]-1]);
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.tableView numberOfRowsInSection:[self.tableView numberOfSections]-1]-1
inSection:[self.tableView numberOfSections] - 1]
atScrollPosition:UITableViewScrollPositionBottom
animated:YES];
创建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
})