This post is closely related to my previous post: TDBadgedCell keeps caching the BadgeNumber
The "badge" from TDBadgedCell keeps caching the numbers. A very simple example is shown he开发者_如何学Gore:
- (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;
}
Anyone has any clue why this happens? The textLabel and detailTextLabel don't cache the data. Any additonal info would be welcome as well, as I seem to have a lot of issues with the caching of graphics in UITableViewCells. Any best practices or other useful information would be most welcome.
OK, I figured this one out. Apparently I shouldn't use the default code to initialize my cell when using the TDBadgedCell. The following code:
TDBadgedCell *cell = (TDBadgedCell *)[_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
Needs to be changed into this:
TDBadgedCell *cell = [[[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
I am wondering if this is clean in terms of memory usage and such, but it'll do for now at least.
I thought this was fixed in commit 2d255f075fe53ad10afe8eb65666207a8f2c65d0, which was made on March 22, 2013. In my case, this initially seemed to fix the issue most of the time, but I still saw cached badges occasionally. Then I realized that you can fix this once-and-for-all by using two different cells: when you need a badged cell, dequeue a TDBadgedCell, and when you don't need a badge, dequeue an ordinary UITableViewCell.
精彩评论