TDBadgedCell keeps caching the BadgeNumber

2019-06-12 11:57发布

For a project I'm working on I'm using the TDBadgedCell as written by Tim Davies. While the cell works fine for the most part, the BadgeNumber keeps getting cached. I'm wondering how I can fix this.

My [tableView:cellForRowAtIndexPath] message looks like this:

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    TDBadgedCell *cell = (TDBadgedCell *)[_tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    Contact *contact = [contactArray objectAtIndex:[indexPath row]];

    if (cell == nil) {
        cell = [[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }

    [cell setBadgeNumber:[[contact unreadMessageCount] intValue]];

    UIImage *statusImage = nil;
    if ([[contact status] isEqualToString:@"online"]) {
            statusImage = [[StatusController sharedInstance] imageForStatus:Online];
    } else if ([[contact status] isEqualToString:@"away"]) {
            statusImage = [[StatusController sharedInstance] imageForStatus:Away];  
 }

    [[cell imageView] setImage:statusImage]; 

    NSString *displayName = [contact displayName];
    [[cell textLabel] setText:displayName];

    NSString *phone = [contact phone];
    [[cell detailTextLabel] setText:phone];

    [cell setBadgeNumber:[[contact unreadMessageCount] intValue]];

    return cell;
}

The text and image gets redrawn correctly when I scroll up and down. The badgenumber doesn't however. Any suggestions on how to fix this would be welcome.

1条回答
Root(大扎)
2楼-- · 2019-06-12 12:37

Try calling "setNeedsDisplay" on the cell after you've set the badgeNumber to force the cell to redraw.

查看更多
登录 后发表回答