jim
jim

Reputation: 1025

change the background color of customize tableViewCell

I want to change my cell's background color

I see other question but they are not correspond my situation

I want to do a notification tableView

for example,If user have read cell, the cell's background color will change to white

If not, the color is yellow

At beginning

I set color in

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

   if(read[[indexPath row]])
     [cell.backView setBckgroundColor:[UIColor whiteColor];
   else
     [cell.backView setBckgroundColor:[UIColor redColor];
}

It works, and then i want to change color in

tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
   read[[indexPath row]] = yes;
   [cell.backView setBckgroundColor:[UIColor whiteColor];
   cell.name.text = @"test";
}

it works too

BUT if i selection other cell, it change to orignial color

It seems it only can change ONE cell's color at same time

No matter I use

cell.backgroundColor
cell.contentView.backgroundColor
cell.backView 

it get the same result,can anyone help me


edit 4/20 20:13

I set read in

tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath

sorry for misdirection,i update code

and after i selection other cell, i don't call [tableView reload]

so i don't think this is the reason

By the way, everything(e.g label) can change but background color

and if i select cell , it jump to other screen by navigation


Ans

conclusion first

tableView: cellForRowAtIndexPath: is well

and

tableView: willDisplayCell: is well too

both of them can change background color

but they execute when need to draw new cell or reload tableView

I still confuse why I can change label in didselectionRowAtIndexPath but i can't change color

Upvotes: 1

Views: 321

Answers (6)

Tanveer Mujawar
Tanveer Mujawar

Reputation: 25

Have you called reloadData for the tableView Set read property in the "didSelectRowAtIndexPath" and reload the tableView

Upvotes: 0

Tanveer Mujawar
Tanveer Mujawar

Reputation: 25

Try to get the Selected cell in the didSelectRowAtIndexPath

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

//set read property for the seleted index

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];

}

Upvotes: 0

Chez Hung
Chez Hung

Reputation: 281

In your edited code, when you select the cell, you set the read flag to YES and change the color to white, that's fine, but in your cellForRowAtIndexPath: you set cells with the read flag YES to red, and NO to white, which is opposite to the behaviour in your didSelectRowAtIndexPath: method.

Upvotes: 0

SleepNot
SleepNot

Reputation: 3038

You need to update the "read" property of the object at the index of your cell.

tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
   [cell.backView setBckgroundColor:[UIColor whiteColor];
   currentObjectForThisCell.read = YES;
}

then:

-(void)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
   if(currentObjectForThisCell.read)
     [cell.backView setBckgroundColor:[UIColor whiteColor];
   else
     [cell.backView setBckgroundColor:[UIColor redColor];
}

Upvotes: 0

Chez Hung
Chez Hung

Reputation: 281

I think you want to set your variable "read" to YES in

tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath

and I guess you use only one variable to record the states just because you simplified it for posting here, otherwise you might want to record the state of each cell with separate variables.

Upvotes: 0

Yuyutsu
Yuyutsu

Reputation: 2527

Change color using tableView: willDisplayCell: method

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (...){ //do your stuff.
        [cell.backView setBckgroundColor:[UIColor redColor];
    } else {
        [cell.backView setBckgroundColor:[UIColor whiteColor];
    }
}

Upvotes: 2

Related Questions