ROSSiDEAS
ROSSiDEAS

Reputation: 21

Color change specific to only 1 tableViewCell item replicates every 10 items

I want the first item of my tableView to be a different color than the rest of the list.

so I:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
{

    static NSString *CellIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = [_content objectAtIndex:indexPath.row];

    if (indexPath.row==0){
        cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0.6 alpha:1];
    }
        return cell;
}

Upvotes: 2

Views: 40

Answers (1)

Devin
Devin

Reputation: 900

UITableView's do not allocate a new cell for every row. Instead, to save on memory, you call dequeueReusableCellWithIdentifier:, which grabs a cell that has previously been allocated. What I suspect is happening is that your screen can hold 10 cells, and so when your first cell scrolls off screen, it gets reused for cell 11, but its text color remains the same. To fix this, simply add an else statement to your color assignment.

if (indexPath.row==0){
    cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0.6 alpha:1];
}
else {
    cell.textLabel.textColor = [UIColor someOtherColor];
}

That way, when the first cell gets reused, its color will be reset.

Upvotes: 3

Related Questions