【发布时间】:2014-03-20 06:22:11
【问题描述】:
我在 ViewController 中有一个 UITableView,其中包含一个自定义单元格。 当视图第一次加载时,以下代码中的 indexPathForCell 返回 indexPath 没有问题:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PostCell *postCell = (PostCell *)[tableView dequeueReusableCellWithIdentifier:@"PostCell"];
if (postCell == nil) {
NSLog(@"EmptyCell Found");
}
NSDictionary *object = [postArray objectAtIndex:indexPath.row];
NSString *imageURL = [object objectForKey:@"imageURL"];
NSIndexPath *originalIndexPath = indexPath;
NSLog(@"Original IndexPath %@", originalIndexPath);
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:[NSURL URLWithString:imageURL]
options:indexPath.row == 0 ? SDWebImageRefreshCached : 0
progress:^(NSInteger receivedSize, NSInteger expectedSize){}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished){
if (image) {
// This returns correctly on first load
NSIndexPath *currentIndexPath = [imageTableView indexPathForCell:postCell];
NSLog(@"Current IndexPath %@", currentIndexPath);
}
}
];
}
刷新 tableView 后,currentIndexPath 始终为 (null)。 以下是我的刷新代码:
- (void)refreshMainView {
AFHTTPRequestOperation *downloadPostOperation = [[AFHTTPRequestOperation alloc] initWithRequest:serviceRequest];
[downloadPostOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//[tableView beginUpdates];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:urlData options:kNilOptions error:&error];
NSMutableArray *newPostArray = [json objectForKey:@"posts"];
// Replacing the old array with new array
postArray = newPostArray;
[self stopRefresh];
//[tableView endUpdates]
[imageTableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self stopRefresh];
}];
如果我只执行[tableView reloadData]而不执行refreshMainView,那么获取currentIndexPath不会有任何问题。
我必须忽略某些东西,有人可以帮助我吗?谢谢!
编辑:
我已经尝试[postCell.postImageView setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:nil]; 并且刷新工作,所以我怀疑我在完成的块中获取 indexPath 有问题,有人可以帮忙吗?我需要完成的块,因为我正在做一些图像处理。
【问题讨论】:
-
在完成块中调用
beginUpdates和endUpdates是不必要的,因为您不插入或删除单元格。 -
可能是因为
setCompletionBlockWithSuccess块在填充帖子数组之前刷新您的表格视图。 -
嗨 Nitin,你能详细说明一下吗?我试过使用
[self performSelectorOnMainThread:@selector(reloadTableView) withObject:nil waitUntilDone:NO];,但没有帮助。根据link,重载也在completionBlock内。 -
imageURL、postData、newPostArray 变量从何而来?请显示更多代码。
-
@HoanNguyen 嗨,我已经更新了更多代码。
标签: ios uitableview reloaddata uirefreshcontrol