Jayyrus
Jayyrus

Reputation: 13061

How to edit uitableviewcell's background color when the cell is selected

i'm developing an app to show push notification in a uitableview. If notification is not yet shown by user, the cell that print that notification has gray background color. So when view is loaded in

cellForRowAtIndexPath

method i see if notification is not shown changing cell's background to gray and leaving it default if notification is shown

if([shown isEqualToString:@"no"]){
    cell.contentView.backgroundColor = [UIColor lightGrayColor];
    cell.textLabel.backgroundColor = [UIColor lightGrayColor];
    cell.detailTextLabel.backgroundColor = [UIColor lightGrayColor];
}
cell.textLabel.text = [NSString stringWithFormat:@"Notifica del %@",data];
cell.detailTextLabel.text = message;

I want to change color of background in white ( the same style when i don't touch background color on load) when users clicks on cell

i do this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *not = [notification objectAtIndex:indexPath.row];
NSArray *stringArray = [not componentsSeparatedByString:@"---"];
NSString *message = [stringArray objectAtIndex:0];
NSString *data = [stringArray objectAtIndex:1];
NSString *shown = [stringArray objectAtIndex:2];
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor whiteColor];
cell.detailTextLabel.backgroundColor = [UIColor whiteColor];
UIAlertView *popUp = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Notifica del %@",data] message:message delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[popUp show];
[popUp release];
}

the result is that all the cell become totally white ( hiding texts ) and alert appear, but when i click on another cell, the first clicked become again like before and new cell clicked become totally white.. how can i do? can you help me?

Upvotes: 0

Views: 900

Answers (1)

Emon
Emon

Reputation: 958

Make UIView in your cell. Take a UILabel on this UIView. Write the cell text on this UILabel. Now, set that UIView backgroundColor and cell.seletionStyle = none and UILabel backgroundColor = clear.

Now, in:

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

 change your view background color and uilabel color. shortly change color of cellview and cell text color

}

Upvotes: 0

Related Questions