gpt4 book ai didi

iphone - 使用 GCD 重用 UITableViewCell

转载 作者:可可西里 更新时间:2023-11-01 04:42:23 25 4
gpt4 key购买 nike

我正在使用 Grand Central Dispatch异步加载 UITableViewCell 的图像。除了在单元格被重复使用并且前一个 block 加载错误图像的某些边界情况外,这工作得很好。

我当前的代码如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

NSString *imagePath = [self imagePathForIndexPath:indexPath];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

dispatch_async(queue, ^{
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];

dispatch_sync(dispatch_get_main_queue(), ^{
cell.imageView.image = image;
[cell setNeedsLayout];
});
});

return cell;
}

据我所知,GCD 队列无法停止。那么如何才能避免这种边境案件呢?或者我应该使用其他东西而不是 GCD 来解决这个问题?

最佳答案

我所做的是向单元格添加一个 NSOperation ivar。该操作负责获取、加载和创建图像。完成后,它会对单元格执行 ping 操作。当/如果单元格出队时,如果操作尚未完成,则取消并销毁该操作。在-main中取消操作测试。图像传递给单元格后,单元格放弃操作并使用图像。

关于iphone - 使用 GCD 重用 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11930687/

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