gpt4 book ai didi

ios - 重新加载 UITableView 数据后,indexPathForCell 返回 null

转载 作者:行者123 更新时间:2023-11-28 20:04:33 25 4
gpt4 key购买 nike

我在包含自定义单元格的 ViewController 中有一个 UITableView。当 View 首次加载时,以下代码中的 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]; 而不是刷新工作,所以我怀疑我在完成的 block 中获取 indexPath 有问题, 任何人都可以帮忙吗?我需要完成的 block ,因为我正在做一些图像处理。

最佳答案

嗨,我终于修好了。

对于需要答案的人,只需添加

dispatch_async(dispatch_get_main_queue(), ^{});

围绕检索 indexPathForCell 的部分。

关于ios - 重新加载 UITableView 数据后,indexPathForCell 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22524980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com