代码之家  ›  专栏  ›  技术社区  ›  Wolfgang Schreurs

TDBadgedCell缓存问题

  •  2
  • Wolfgang Schreurs  · 技术社区  · 15 年前

    这篇文章与我以前的文章密切相关: TDBadgedCell keeps caching the BadgeNumber

    来自tdbagedcell的“badge”不断缓存这些数字。这里显示了一个非常简单的示例:

    - (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中缓存图形似乎有很多问题。最受欢迎的是任何最佳实践或其他有用信息。

    2 回复  |  直到 12 年前
        1
  •  1
  •   Wolfgang Schreurs    15 年前

    好吧,我想出来了。显然,在使用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];
    

    我想知道在内存使用等方面这是否是干净的,但至少现在是这样。

        2
  •  0
  •   EastgateMike    12 年前

    我以为这是在承诺中解决的 2d255f075fe53ad10afe8eb65666207a8f2c65d0 2013年3月22日。在我的例子中,这在大多数时候似乎可以解决这个问题,但我还是偶尔看到缓存的徽章。然后我意识到你可以通过使用两个不同的单元来解决这个问题:当你需要一个带标记的单元时,将一个TdbackedCell出列;当你不需要一个标记时,将一个普通的UitableViewCell出列。

    推荐文章