Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS: UITableViewCell reuse, image of button changing
    text
    copied!<p>I know this has been widely discussed before, and I'm sure, I'm just missing something simply, but like many others before me, I have an issue with cell reuse in a UITableView... I have a button that displays an avatar, and I asynchronously download it the first time, and then cache it for later use... When I scroll in my tableview while the images are being downloaded the first time, the avatars appear wrongfully in cells being reused, despite that I'm setting the avatar to a default avatar before beginning a download, so I'm hoping someone here will know the solution...</p> <p>In cellForRowAtIndexPath:</p> <pre><code>WaitingCell *waitingCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierWaiting]; if (waitingCell == nil) { waitingCell = [[WaitingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierWaiting]; waitingCell.delegate = self; } [waitingCell.userButton setImage:[UIImage imageNamed:DefaultAvatar] forState:UIControlStateNormal]; [waitingCell.usernameLabel setText:username]; [waitingCell.scoreLabel setText:score]; __block UIImage *img; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(queue, ^{ if([p1.objectId isEqualToString:currentUser.objectId]) { img = [profileImages getProfileImageForUser:[p2 objectId]]; waitingCell.user = p2; } else { img = [profileImages getProfileImageForUser:[p1 objectId]]; waitingCell.user = p1; } dispatch_async(dispatch_get_main_queue(), ^{ [waitingCell.userButton setImage:img forState:UIControlStateNormal]; }); return waitingCell; </code></pre> <p>The image of "userButton" is the part that's getting reused until image is fully cached, but I thought that by setting it to a default value (DefaultAvatar is a constant file name), it would simply set the default avatar until the other was downloaded and it therefore wouldn't reuse other images..</p> <p>The button is defined in my custom UITableViewCell called WaitingCell as such:</p> <pre><code> self.userButton = [UIButton buttonWithType:UIButtonTypeCustom]; [self.userButton setImage:[UIImage imageNamed:PlaceholderAvatar] forState:UIControlStateNormal]; self.userButton.frame = CGRectMake(20, 8, 51, 54); [self.contentView addSubview:self.userButton]; </code></pre> <p>Any ideas how I can keep it from reusing images and simply use the DefaultAvatar until the correct image is downloaded?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload