Search code examples
objective-ciosuitableviewidentifier

Objective C: Cell text disappears after scrolling out of screen (and back)


I am creating a controller with a UITableview which contains 2 sections. In the second section, I have a list of users which I want to add a checkmark to the user in the list when I click on the particular row. I have an issue whereby after I click on a user and scroll the cell out of the page, the cell returns with the 'name' field BLANK (see screenshot below). I know this has to do with the way the cells are reused but am not able to get my head around the exact issue. My code is posted below. Any advise?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* ShareOnCellIdentifier = @"ShareOnholderCell";
    static NSString* WhoShouldAnswerCellIdentifier = @"WhoShouldAnswerCell";

    int row = [indexPath row];

    User *user = [self.friendsToShareWithArray objectAtIndex:row];

    if (indexPath.section == 0) 
    {

        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ShareOnCellIdentifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ShareOnCellIdentifier];
        }
        cell.textLabel.text = [self.shareOnArray objectAtIndex:indexPath.row];

        return cell;

    }
    else
    {
        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:WhoShouldAnswerCellIdentifier];
        if (cell == nil)
        {
             cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:WhoShouldAnswerCellIdentifier];
         }
        cell.textLabel.text = user.nickname;
        if (user.isSelected) 
        {
            cell.selectionStyle = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    User *user = [self.friendsToShareWithArray objectAtIndex:indexPath.row];
    if (indexPath.section == 1) 
    {
        UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];

        if (cell.accessoryType == UITableViewCellAccessoryCheckmark) 
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
            user.isSelected = NO;

        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            user.isSelected = YES;
        }
    }
}

EDIT: Example of how I create my user object

User *user1 = [[User alloc]init];
    user1.nickname = @"Dionis";
    user1.uid = @"1";
    user1.isSelected = NO;

Solution

  • The only problem I see is in the way you've implemented didSelectRowAtIndexPath: method.

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.section == 1) 
        {
            User *user = [self.friendsToShareWithArray objectAtIndex:indexPath.row];
            user.isSelected = !user.isSelected;
    
            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                             withRowAnimation:UITableViewRowAnimationNone];
        }
    }