user3823935
user3823935

Reputation: 891

UITableView row color changing after scrolling

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

static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil){
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    cell.selectionStyle = UITableViewCellStyleDefault;
}
cell.contentView.backgroundColor = [UIColor clearColor];

cell.textLabel.text = @"testing";
if(indexPath.row == 1){
    cell.textLabel.textColor = UIColorMake(kOrangeColour);
}

return cell;

}

I have a tableview with 14 rows in storyboard.I made change color of 2 row (i.e.1 index).When i scroll up and down the tableview for many times then i found the 14 row(i.e 13 index) color also changed.now both the row 1 and 14 are in orange color.As i coded it should change color of only one row.Why this happening?any help will be appreciated. thanks in advance.

Upvotes: 1

Views: 1523

Answers (1)

Goppinath
Goppinath

Reputation: 10739

Use

if(indexPath.row == 1){
    cell.textLabel.textColor = UIColorMake(kOrangeColour);
} else {
    cell.textLabel.textColor = [UIColor whiteColor]; // change is here
}

instead of

if(indexPath.row == 1){
    cell.textLabel.textColor = UIColorMake(kOrangeColour);
}

During reusability of UITableViewCell, you have to declare the if and else.

Upvotes: 5

Related Questions