gpt4 book ai didi

iphone - UITableViewCell 的 subview 偶尔会消失

转载 作者:行者123 更新时间:2023-12-01 19:18:28 27 4
gpt4 key购买 nike

我有一个 UITableViewCell子类,它有一个名为 entryView 的属性,代替只读的 cell.contentView。我以为我正确地使用了表格 View ——记住每个条目都会重复使用单元格,并且每次都需要添加所有内容。然而,偶尔滚动时,entryView 会在单元格上消失。单元格仍然存在,但 entryView 似乎已从 superview 中删除。这是我的 cellForRowAtIndexPath:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
JTimelineCell *cell = (JTimelineCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[JTimelineCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];

}

JTimelineCellContentView *contentView;

Entry *entry = [_fetchedResultsController objectAtIndexPath:indexPath];

if ([self.cellContentCache objectForKey:entry.entryID]) {
contentView = [self.cellContentCache objectForKey:entry.entryID];

contentView.frame = cell.bounds;
[contentView setNeedsDisplay];
}
else {

contentView = [[JTimelineCellContentView alloc] initWithFrame:cell.bounds];


[self.cellContentCache setObject:contentView forKey:entry.entryID];

contentView.time = [NSDate timeStringForTime:entry.creationDate];

contentView.message = entry.message;

[cell setNeedsDisplay];
}

[cell.entryView removeFromSuperview];
cell.entryView = contentView;

[cell addSubview:cell.entryView];

return cell;
}

最佳答案

我相信您在另一个问题中发布了类似的内容。您不能缓存单元格的 contentView。您可以缓存图像、字符串、数字、值等 - 但最终,当您获得一个单元格时,您需要检索将显示这些值的元素,然后设置它们的值来执行此操作。因此,您可以缓存图像、字符串、CGRect 等 - 然后当被要求填充单元格时,您可以查看缓存,如果有效则获取您缓存的值。如果未设置缓存,则需要从头开始创建它们。

每次您获得特定部分/行的单元格时,它都会有所不同。它可能包含旧值,也可能是新值。您需要为每个项目设置每个 GUI 元素。一种便于找到这些元素的方法是给每个元素一个唯一的标签,这样您就可以使用 [cell.contentView viewForTag:xxx] 来获取项目,然后设置它。

关于iphone - UITableViewCell 的 subview 偶尔会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11552238/

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