Jose
Jose

Reputation: 93

Modifying one cell in CellForRowAtIndexPath changes multiple cells

I have an array of names that I'm showing via tableview. You can select up to a total of 3 names, and you cannot re-select the same names. To do this I implemented the following code in cellForRowAtIndexPath:. When I run the code the names come up fine, but there are multiple red cells with names that I did not select.

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

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    NSString *sectionTitle = [nameSectionTitles objectAtIndex:indexPath.section];
    NSArray *sectionNames = [names objectForKey:sectionTitle];
    NSString *name = [sectionNames objectAtIndex:indexPath.row];
    cell.textLabel.text = name;

    if ([name isEqualToString: self.name1] || [name isEqualToString: self.name2] || [name isEqualToString: self.name3]) {
        [cell setUserInteractionEnabled:NO];
        cell.backgroundColor = [UIColor redColor];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

Reading a similar problem here, they were saying that it's because the cells are being reused - but if this were true, how is the tableview still displaying the correct names in the correct position?

I tried to simplify the code into this, and still to no avail, there were multiple red cells.

myIP = [NSIndexPath indexPathForRow:0 inSection:0];


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

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    NSString *sectionTitle = [nameSectionTitles objectAtIndex:indexPath.section];
    NSArray *sectionNames = [names objectForKey:sectionTitle];
    NSString *name = [sectionNames objectAtIndex:indexPath.row];
    cell.textLabel.text = name;

    if (indexPath == myIP) {
        cell.backgroundColor = [UIColor redColor];
    }

    return cell;
}

I can post a screenshot if needed. Note: The intended names were correctly labeled with red.

Upvotes: 2

Views: 350

Answers (1)

Midhun MP
Midhun MP

Reputation: 107141

The issue is happening due to cell re-using. When a cell with red background is re-used it'll still be in red background, you are not re-setting it anywhere in your code. You need to put a else case to your if condition used in cellForRowAtIndexPath: method.

if ([name isEqualToString: self.name1] || [name isEqualToString: self.name2] || [name isEqualToString: self.name3])
{
    [cell setUserInteractionEnabled:NO];
    cell.backgroundColor = [UIColor redColor];
    cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
    [cell setUserInteractionEnabled:YES];
    cell.backgroundColor = [UIColor clearColor];
    // Other stuffs
}

Upvotes: 4

Related Questions