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.
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.
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?)
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];
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]; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With