Reputation: 3977
I am trying to set up some UIButtons and UILabels in my custom UITableViewCell. For each row in the table I have 4 buttons with user profile images and below each button is a label displaying the username. Images and usernames are taken from Parse.com. I have a friends array of size 34, and I am displaying 9 rows so the last two buttons and label must be hidden. The code below works but for some reason when I scroll up the table some of the other rows will also hide their two rightmost buttons and labels. I'm wondering if my logic for loading images from the array is incorrect. Not sure what's going on here. Any advice would be greatly appreciated.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
FriendViewCell *cell = (FriendViewCell *)[tableView dequeueReusableCellWithIdentifier:@"friendCell" forIndexPath:indexPath];
for (int i = 0; i < 4; i++) {
if ((int)indexPath.row * 4 + i < [self.currentUser.friends count]) {
UIButton *button = cell.buttons[i];
[button setTag:(int)indexPath.row * 4 + i];
button.layer.cornerRadius = button.frame.size.width / 2;
button.clipsToBounds = YES;
UILabel *label = cell.labels[i];
[button addTarget:self action:@selector(friendTapped:) forControlEvents:UIControlEventTouchUpInside];
//here we need to decide what to access
PFUser *user = self.currentUser.friends[(int)indexPath.row * 4 + i];
label.text = user.username;
PFFile *userImageFile = user[@"profilePic"];
[userImageFile getDataInBackgroundWithBlock: ^(NSData *imageData, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:imageData];
[button setBackgroundImage:image forState:UIControlStateNormal];
}
}];
}
else {
UIButton *button = cell.buttons[i];
[button setEnabled:NO];
[button setHidden:YES];
UILabel *label = cell.labels[i];
[label setHidden:YES];
}
}
return cell;
}
Upvotes: 3
Views: 749
Reputation: 2958
The 'iOS' way to handle this would be to fill out the prepareForReuse method of your custom cell. This is where you should be reseting various attributes on cells that may be reused by the caching system.
-prepareForReuse
Prepares a reusable cell for reuse by the table view's delegate.
Discussion
If a UITableViewCell object is reusable—that is, it has a reuse identifier—this method is invoked just before the object is returned from the UITableView method dequeueReusableCellWithIdentifier:. For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell. If the cell object does not have an associated reuse identifier, this method is not called. If you override this method, you must be sure to invoke the superclass implementation.
Upvotes: 0
Reputation: 4584
You can try by unhiding the button and Label in your first conduction like:
if ((int)indexPath.row * 4 + i < [self.currentUser.friends count]) {
[button setEnabled:YES];
[button setHidden:NO];
[label setHidden:NO];
// Other code
}
else{
......
}
Upvotes: 1