Ala*_*lan 167 objective-c uitableview reloaddata
我想在完成表演后滚动到UITableView的底部 [self.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];
Run Code Online (Sandbox Code Playgroud)
但后来我读了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);
Run Code Online (Sandbox Code Playgroud)
在控制台中它返回Sections = 1,Row = -1;
当我完成相同的NSLogs时,cellForRowAtIndexPath我得到Sections = 1和Row = 8; (8是对的)
rob*_*off 273
接下来的布局传递,当您返回控制运行循环(后,说,你的按钮操作或任何返回),这通常发生在重载情况.
因此,在表视图重新加载后运行某些东西的一种方法就是强制表视图立即执行布局:
[self.tableView reloadData];
[self.tableView layoutIfNeeded];
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];
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用以下方法安排后布局代码以便以后运行dispatch_async:
[self.tableView reloadData];
dispatch_async(dispatch_get_main_queue(), ^{
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];
});
Run Code Online (Sandbox Code Playgroud)
经进一步调查,我发现表视图将tableView:numberOfSections:和tableView:numberOfRowsInSection:它的数据源来自返回之前reloadData.如果委托实现tableView:heightForRowAtIndexPath:,表视图也会在返回之前发送(对于每一行)reloadData.
但是,表视图不会发送tableView:cellForRowAtIndexPath:或tableView:headerViewForSection直到布局阶段,默认情况下,当您将控制返回到运行循环时.
我还发现,在一个很小的测试程序中,你问题中的代码正确地滚动到表视图的底部,没有我做任何特殊的事情(比如发送layoutIfNeeded或使用dispatch_async).
Avi*_*oss 100
迅速:
extension UITableView {
func reloadData(completion: ()->()) {
UIView.animateWithDuration(0, animations: { self.reloadData() })
{ _ in completion() }
}
}
...somewhere later...
tableView.reloadData {
println("done")
}
Run Code Online (Sandbox Code Playgroud)
Objective-C的:
[UIView animateWithDuration:0 animations:^{
[myTableView reloadData];
} completion:^(BOOL finished) {
//Do something after that...
}];
Run Code Online (Sandbox Code Playgroud)
小智 43
从Xcode 8.2.1,iOS 10和swift 3开始,
您可以tableView.reloadData()使用CATransaction块轻松确定结束:
CATransaction.begin()
CATransaction.setCompletionBlock({
print("reload completed")
//Your completion code here
})
print("reloading")
tableView.reloadData()
CATransaction.commit()
Run Code Online (Sandbox Code Playgroud)
以上也适用于确定UICollectionView的reloadData()和UIPickerView的reloadAllComponents()的结束.
Tyl*_*fer 31
上述dispatch_async(dispatch_get_main_queue())方法无法保证正常工作.我看到它的非确定性行为,有时系统已经完成了layoutSubviews和完成块之前的单元格渲染,有时甚至完成.
这是一个在iOS 10上为我100%工作的解决方案.它需要能够将UITableView或UICollectionView实例化为自定义子类.这是UICollectionView解决方案,但它与UITableView完全相同:
CustomCollectionView.h:
#import <UIKit/UIKit.h>
@interface CustomCollectionView: UICollectionView
- (void)reloadDataWithCompletion:(void (^)(void))completionBlock;
@end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
用法示例:
[self.collectionView reloadDataWithCompletion:^{
// reloadData is guaranteed to have completed
}];
Run Code Online (Sandbox Code Playgroud)
请参阅此处获取此答案的Swift版本
小智 27
我和Tyler Sheaffer有同样的问题.
我在Swift中实现了他的解决方案,它解决了我的问题.
Swift 3.0:
final class UITableViewWithReloadCompletion: UITableView {
private var reloadDataCompletionBlock: (() -> Void)?
override func layoutSubviews() {
super.layoutSubviews()
reloadDataCompletionBlock?()
reloadDataCompletionBlock = nil
}
func reloadDataWithCompletion(completion: @escaping () -> Void) {
reloadDataCompletionBlock = completion
self.reloadData()
}
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特2:
class UITableViewWithReloadCompletion: UITableView {
var reloadDataCompletionBlock: (() -> Void)?
override func layoutSubviews() {
super.layoutSubviews()
self.reloadDataCompletionBlock?()
self.reloadDataCompletionBlock = nil
}
func reloadDataWithCompletion(completion:() -> Void) {
reloadDataCompletionBlock = completion
self.reloadData()
}
}
Run Code Online (Sandbox Code Playgroud)
用法示例:
tableView.reloadDataWithCompletion() {
// reloadData is guaranteed to have completed
}
Run Code Online (Sandbox Code Playgroud)
看来人们仍在阅读这个问题和答案.B/c,我正在编辑我的答案,删除Synchronous这个与此无关的词.
When [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];
Run Code Online (Sandbox Code Playgroud)
还有一个UICollectionView基于kolaworld答案的版本:
需要测试.到目前为止,在iOS 9.2,Xcode 9.2 beta 2上工作,将collectionView滚动到索引,作为闭包.
extension UICollectionView
{
/// Calls reloadsData() on self, and ensures that the given closure is
/// called after reloadData() has been completed.
///
/// Discussion: reloadData() appears to be asynchronous. i.e. the
/// reloading actually happens during the next layout pass. So, doing
/// things like scrolling the collectionView immediately after a
/// call to reloadData() can cause trouble.
///
/// This method uses CATransaction to schedule the closure.
func reloadDataThenPerform(_ closure: @escaping (() -> Void))
{
CATransaction.begin()
CATransaction.setCompletionBlock(closure)
self.reloadData()
CATransaction.commit()
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
myCollectionView.reloadDataThenPerform {
myCollectionView.scrollToItem(at: indexPath,
at: .centeredVertically,
animated: true)
}
Run Code Online (Sandbox Code Playgroud)
我使用这个技巧,很确定我已将它发布到这个问题的副本:
-(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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
107735 次 |
| 最近记录: |