Uitableview TDBadgedCell的缓存问题
这篇文章与我以前的文章密切相关: TDBadgedCell的“徽章”将继续缓存数字。此处显示了一个非常简单的示例:Uitableview TDBadgedCell的缓存问题,uitableview,Uitableview,这篇文章与我以前的文章密切相关: TDBadgedCell的“徽章”将继续缓存数字。此处显示了一个非常简单的示例: - (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; TDBadgedCell *cell = (TDBadgedCell
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
TDBadgedCell *cell = (TDBadgedCell *)[_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
[cell setBadgeColor:[UIColor blackColor]];
[cell setBadgeNumber:[indexPath row]];
[[cell textLabel] setText:[NSString stringWithFormat:%@"%d", [indexPath row]]];
return cell;
}
有人知道为什么会这样吗?textLabel和detailTextLabel不会缓存数据。任何附加信息都是受欢迎的,因为我似乎对UITableViewCells中的图形缓存有很多问题。欢迎提供任何最佳实践或其他有用信息。好的,我找到了这个。显然,在使用TDBadgedCell时,我不应该使用默认代码来初始化我的单元格。以下代码:
TDBadgedCell *cell = (TDBadgedCell *)[_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
需要更改为:
TDBadgedCell *cell = [[[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
我想知道这在内存使用等方面是否是干净的,但至少现在可以了。我认为这在2013年3月22日的提交中已经修复。在我的例子中,这在大多数情况下似乎可以解决问题,但我仍然偶尔看到缓存的徽章。然后我意识到,您可以通过使用两个不同的单元来一次性解决这个问题:当您需要标记的单元时,将TDBadgedCell出列;当您不需要标记时,将普通UITableViewCell出列