简体   繁体   中英

UITableViewCell randomly disappears + scrolling performance issue on iPhone 4

To sum it up, I'm facing 2 problems, the 1st is that time to time some cells disappear from the tableview and reappear randomly. The 2nd is a scrolling performance issue on iPhone 4 and below.

1st problem :

I am displaying a simple list of elements in an UITableView by adding a subview the [cell contentView]. Most of the time it works well, but sometimes when i scroll (and really randomly) a cell disappears.

Because this cell which "disappeared" is reused, the reused cell will also be "blank" when I scroll. And randomly again a cell can reappear/disappear while scrolling.

Here is my code for the cellForRowAtIndexPath: method :

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
LBSelectorChampionViewController *item = [self itemAtIndexPath:indexPath];
if (cell == nil) {
    NSLog(@"CELL NIL");
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    [[item view] setTag:99];
    [[item champName] setTag:50];
    [[item champImage] setTag:51];
    [[item buttonFavorite] setTag:52];
    [[cell contentView] addSubview:[item view]];
}

UILabel *championLabel =  (UILabel*)[[[cell contentView] viewWithTag:99] viewWithTag:50];
LBFavoriteChampionButton *buttonFavorite = (LBFavoriteChampionButton*)[[[cell contentView] viewWithTag:99] viewWithTag:52];
UIImageView *championImage = (UIImageView*)[[[cell contentView] viewWithTag:99] viewWithTag:51];

// Text
[championLabel setText:[item displayValue]];

[championImage setImageWithURL:[NSURL URLWithString:[item imageUrl]]];

// Fav button
[buttonFavorite setCurrentChampion:item];
if ([[self favoritesChampions] containsObject:item]) {
    [buttonFavorite setSelected:YES];
    [buttonFavorite setIsFavorite:YES];
} else {
    [buttonFavorite setSelected:NO];
    [buttonFavorite setIsFavorite:NO];
}
return cell;}
}

I've tried to log everything, I checked if "item" could be sometimes null and it's never the case. But when a cell disappears, the [[[cell contentView] subviews] count] is equal to 0, which normally should be 1. I really don't understand what's happening here. I tested it on real devices + simulator and it happens with both of them.

2nt problem :

My other "problem" is that the scrolling of the tableview is not as smooth as i would expect it to be on the iPhone 4 (tested on iOS 6 and iOS 7, same result :-( ). I'm using SDWebImage to load images asynchronously and to cache them, I'm reusing cells, what could I do to improve the performances ? I tested on an iPhone 4S and had no problem, scrolling is very smooth.

Am I doing something wrong ? Any ideas about one of my problem ?

Thank you for taking the time to answer me.

EDIT : 1st attempt to solve the problem

Trying to customize cell with a custom UITableViewCell subclass :

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"LBListChampionCell";

LBListChampionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil){
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"LBListChampionCell" owner:self options:nil];
    cell = [topLevelObjects objectAtIndex:0];
}
LBSelectorChampionViewController *item = [self itemAtIndexPath:indexPath];
[[cell championName] setText:[item displayValue]];
[[cell championLogo] setImageWithURL:[NSURL URLWithString:[item imageUrl]]];
return cell;
}

I wasn't able for the moment to reproduce the "disappear" bug (sometimes it takes a moment to reproduce it...), but there is no improvement at all concerning the performances :(. Even while just setting a dummy text : [[cell championName] setText:@"Test"]; (and commenting the item part) the scrolling is still not really smooth.

Any ideas ?

EDIT 2 : Best solution (thanks to rdelmar)

Create a subclass of UITableViewCell and load the nib in viewDidLoad :

- (void)viewDidLoad
{
    ...
    UINib *nib = [UINib nibWithNibName:@"LBListChampionCell" bundle:nil];
    [[self tableView] registerNib:nib forCellReuseIdentifier:@"LBListChampionCell"];
}

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LBListChampionCell *cell =  [tableView dequeueReusableCellWithIdentifier:@"LBListChampionCell"];
    LBSelectorChampionViewController *item = [self itemAtIndexPath:indexPath];
    [[cell championName] setText:[item displayValue]];
    [[cell championLogo] setImageWithURL:[NSURL URLWithString:[item imageUrl]]];
    ...
}

It increased the scrolling performance (still not perfectly smooth on iPhone 4 but it works well). Moreover it seems that no cell are disappearing anymore :-)

Thanks rdelmar!

I think somehow LBSelectorChampionViewController *item = [self itemAtIndexPath:indexPath]; from one of your cell's contentView was added to another cell's contentView, which would cause the previous cell's contentView to have 0 subview.

I think you are getting the first problem because of cell reusability. LBSelectorChampionViewController *item = [self itemAtIndexPath:indexPath]; will not always be added to the [cell contentView] as cell may not be nil. In addition, there might be a situation when this view will be added to another cell and removed from the previous. Anyway, this will be easier to fix if you subclass UITableViewCell and make LBSelectorChampionViewController part of this custom cell. This will make everything easier and you will never meet reusability problems.

I am not exactly familiar with SDWebImage, but maybe the problem is that it doesn't cache images and tries to load new image every time [championImage setImageWithURL:[NSURL URLWithString:[item imageUrl]]]; is hit. I've used AFNetworking in a few projects to load images and have never had any problems with it.

Hope this is helpful! Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM