Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing background color of selected cell?

Does anyone know how to change the background color of a cell using UITableViewCell, for each selected cell? I created this UITableViewCell inside the code for TableView.

like image 573
sg. Avatar asked Mar 10 '10 15:03

sg.


People also ask

How do I make the background of a cell in Excel a different color?

Fill cells with patternsOn the Home tab, in the Font group, click the Format Cells dialog box launcher. Keyboard shortcut You can also press CTRL+SHIFT+F. In the Format Cells dialog box, on the Fill tab, under Background Color, click the background color that you want to use.

How do I change the background color of a selected cell in Swift?

Swift 3, 4, 5 select cell background colour Next connect your cell's selectedBackgroundView Outlet to this view. You can even connect multiple cells' outlets to this one view. Show activity on this post. For a solution that works (properly) with UIAppearance for iOS 7 (and higher?)


2 Answers

Changing the property selectedBackgroundView is correct and the simplest way. I use the following code to change the selection color:

// set selection color UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame]; myBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]; cell.selectedBackgroundView = myBackView; [myBackView release]; 
like image 147
Chilly Zhong Avatar answered Sep 20 '22 04:09

Chilly Zhong


I finally managed to get this to work in a table view with style set to Grouped.

First set the selectionStyle property of all cells to UITableViewCellSelectionStyleNone.

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

Then implement the following in your table view delegate:

static NSColor *SelectedCellBGColor = ...; static NSColor *NotSelectedCellBGColor = ...;  - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {     NSIndexPath *currentSelectedIndexPath = [tableView indexPathForSelectedRow];     if (currentSelectedIndexPath != nil)     {         [[tableView cellForRowAtIndexPath:currentSelectedIndexPath] setBackgroundColor:NotSelectedCellBGColor];     }      return indexPath; }  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {     [[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:SelectedCellBGColor]; }  - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {     if (cell.isSelected == YES)     {         [cell setBackgroundColor:SelectedCellBGColor];     }     else     {         [cell setBackgroundColor:NotSelectedCellBGColor];     } } 
like image 21
loomer Avatar answered Sep 19 '22 04:09

loomer