我有一个UITableViewCell
子类,它有一个名为 entryView 的属性来代替只读的 cell.contentView。我以为我正确地使用了表格视图——记住每个条目都会重复使用单元格,并且每次都需要添加所有内容。然而,偶尔滚动时,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;
}