我在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有问题,请问有人能帮忙吗?我需要完整的块,因为我正在做一些图像处理。
你好,我终于修好了。
对于需要答案的人,只需添加
dispatch_async(dispatch_get_main_queue(), ^{
});
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PostCell *postCell = (PostCell *)[tableView dequeueReusableCellWithIdentifier:@"PostCell"];
if (cell == nil)
{
nib = [[NSBundle mainBundle] loadNibNamed:@"PostCell" owner:self options:nil];
}
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);
}
}
];
使用这个,它可能会帮助你