【发布时间】:2014-08-04 00:50:53
【问题描述】:
我已经实现了一个 UITableView 加载更多功能。 tableView 从有时很慢的服务器加载大图像。我正在为每个图像启动一个 URLConnection 并重新加载与 URLConnection 对应的 indexPath(与连接对象一起保存)。连接本身在 tableView 上调用-reloadData。
现在,当单击加载更多按钮时,我滚动到位置底部的新数据集的第一行。这很好用,也是我的异步加载系统。
我遇到了以下问题:当连接“太快”时,tableView 正在重新加载给定 indexPath 的数据,而 tableView 仍在滚动到新数据集的第一个单元格,tableView 向后滚动一半该单元格的高度。
这就是它的外观和实际作用:
^^^^^^^^^^^^ should ^^^^^^^^^^^^ ^^^^^^^^^^^^^ does ^^^^^^^^^^^^^
这里有一些代码:
[[self tableView] beginUpdates];
for (NSMutableDictionary *post in object) {
[_dataSource addObject:post];
[[self tableView] insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:[_dataSource indexOfObject:post] inSection:0]] withRowAnimation:UITableViewRowAnimationBottom];
}
[[self tableView] endUpdates];
[[self tableView] scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[_dataSource indexOfObject:[object firstObject]] inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
-tableView:cellForRowAtIndexPath: 如果数据源数组中的对象是字符串,则启动 JWURLConnection,并在完成块中将其替换为 UIImage 的实例。然后它重新加载给定的单元格:
id image = [post objectForKey:@"thumbnail_image"];
if ([image isKindOfClass:[NSString class]]) {
JWURLConnection *connection = [JWURLConnection connectionWithGETRequestToURL:[NSURL URLWithString:image] delegate:nil startImmediately:NO];
[connection setFinished:^(NSData *data, NSStringEncoding encoding) {
[post setObject:[UIImage imageWithData:data] forKey:@"thumbnail_image"];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}];
[cell startLoading];
[connection start];
}
else if ([image isKindOfClass:[UIImage class]]) {
[cell stopLoading];
[cell setImage:image];
}
else {
[cell setImage:nil];
}
我可以在 tableView 滚动完成之前阻止 tableView 执行-reloadRowsAtIndexPaths:withRowAnimation: 调用吗?或者您能想出一种防止这种行为的好方法吗?
【问题讨论】:
标签: ios objective-c uitableview reloaddata