change the background color of customize tableViewCell - ios

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

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];
}
}

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.

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];
}

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.

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];
}

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

Related

Changing the highlighted color of textfield text on the custom cell of a tableview

I have a tableview with custom cells. The custom cells have textfields on them.
When a cell is highlighted, I would like to change the color of the text of the textfields from white to black.
I know about cell.textLabel.highlightedTextColor, but can anyone think of a way to change it for a textfield?
Thanks.
You could implement the delegate protocol method (available from iOS 6.0) tableView:shouldHighlightRowAtIndexPath: or tableView:didHighlightRowAtIndexPath: , depending on your requirements, to intercept the users touch and from there obtain a pointer to the UITextField of interest.
The following assumes to be inside an object that is both the table views delegate and datasource.
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
// get the cell by calling datasource protocol method
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
// fast enumeration to get UITextField(s) from the cell
for (UITextField *textField in cell.subviews) {
// change color here...
textField.textColor = [UIColor redColor];
}
return YES;
}

how to create different view for last visible cell of tableview

I want to show last visible cell with different background color (say green).
I am using this code, It works good until I scroll up/down. When I scroll up/down it gives many cell with green background. (I guess this happening due to "dequeueReusableCellWithIdentifier").
Can anyone help me out where I am doing wrong or what is the best way of doing this.
NSArray* indexPaths = [tableView indexPathsForVisibleRows];
if (indexPath.row==[indexPaths count]-1)
{
UIView *v=[[UIView alloc]initWithFrame:cell.bounds];
v.backgroundColor=[UIColor greenColor];
cell.backgroundView=v;
}
Thanks in advance.
Firstly, you shouldn't be allocating anything in cellForRowAtIndexPath - it is bad for performance. You should only be configuring views and controls that already exist for every cell. Because cells are reused, your cell that you add a background view to will get re-used too... and its background view.
In your case, you probably just want to set:
if (lastCell)
cell.contentView.backgroundColor = [UIColor greenColor];
else
cell.contentView.backgroundColor = [UIColor clearColor]; //or whatever color
contentView is already a view every UITableViewCell has for free, so just use that for background color. Apple probably intended it to be used for this case, amongst others.
You should nil the background view before the test.
cell.backgroundView = nil;
if (indexPath.row==[indexPaths count]-1)
{
UIView *v=[[UIView alloc]initWithFrame:cell.bounds];
v.backgroundColor=[UIColor greenColor];
cell.backgroundView=v;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==[yourtableviewarray count]-1)
{
UIView *v=[[UIView alloc]initWithFrame:cell.bounds];
v.backgroundColor=[UIColor greenColor];
cell.backgroundView=v;
}
}

TableView Cell alpha trouble

In my project I have tableview controller with Static Cells.
I need to set alpha for cells.
I do it with this code:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setAlpha:0.25f];
}
But my problem is that [cell setAlpha: 0.25f] also affects the textLabel.
Please, help, how can I fix it?
You can set the background colour of your cell:
cell.contentView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.25.f];
This will set your cell background colour to white, and its alpha to 0.25.
Hope it helps.
The textLabel is a subview of your cell, so it's normal that if you set the cell alpha, its subviews are impacted to. The way you can solved this is by making a custom cell where the text label won't be a subview of your background view (the one you want to set an alpha on) so it's not impacted by the alpha.
Let's try:
// i do it in cellForRowAtIndexPath
cell.contentView.alpha = 0;

Set specific Frame for Background Color by Cell click

i have the following Custom Cell. On the left is a blue label with an image on it, and on the right side a label with some text. No i want that the cell gets highlighted when i click on it, BUT only the white part behind the label should get highlighted, not the whole cell with the blue label and the image, any ideas?
I tried something like this:
categorieCell.selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(100, 0, categorieCell.frame.size.width, categorieCell.frame.size.width)];
categorieCell.selectedBackgroundView.backgroundColor = [UIColor redColor];
If you don't want the cell to be highlighted as a result of the selection, just set the cell's selectionStyle property to UITableViewCellSelectionStyleNone. That will disable overall cell's highlight when it gets selected.
In tableView cellForRowAtIndexPath method apply the following code:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
After that you can change cell background upon the cell gets selected.
In tableView didSelectRowAtIndexPath method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *categorieCell = [tableView cellForRowAtIndexPath:indexPath];
categorieCell.backgroundColor = [UIColor redColor];
}
Don't forget to change cell background when it gets deselected in willDeselectRowAtIndexPath:

Changing the background colour of tableview cell in iOS7

I'm having an issue that's appeared since upgrading to iOS7 in which when I try to change the background colour of a specific tableview cell, it doesn't colour the correct cells (usually the specified ones in addition to other ones). As you can see from my code below, I define the type that I want to be highlighted and then change the colour. It worked perfectly prior to the iOS upgrade so I'm not exactly sure what change has been made that's causing this:
Quick edit: also, when I scroll down the tableview and then back up, it colours more cells that weren't coloured when the tableview controller first loads (if that helps at all).
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* type=[[self.HandPointer.player_hand objectAtIndex:indexPath.row]cardType];
if ([type isEqualToString:#"ace"]){
cell.backgroundColor = [UIColor colorWithRed:0.81 green:0.91 blue:0.81 alpha:1.0];
}
}
I think doing cell customization in tableView: cellForRowAtIndexPath: method is better. In this method,
if ([type isEqualToString:#"ace"])
{
cell.backgroundColor = [UIColor aceColor];
}
else // this else is important. If you add this, scrolling works fine.
{
cell.backgroundColor = [UIColor otherCellColor];
}
You likely have a single re-usable cell style. Consider having a re-usable cell style for your aces, and one for all others. Set the background color in cellForRowAtIndexPath, not willDisplayCell.
pseudo-code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
.
.
.
NSString* type=[[self.HandPointer.player_hand objectAtIndex:indexPath.row]cardType];
if ([type isEqualToString:#"ace"]){
{
// load a cell with the background color desired
cell =
cell.backgroundColor =
.
.
.
return (cell);
}
// else a normal cell
cell =
.
.
.
}

Resources