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.
Here is a quick way to do this right in Interface Builder (within a Storyboard). Drag a simple UIView to the top of your UITableView as in
Next connect your cell's selectedBackgroundView
Outlet to this view. You can even connect multiple cells' outlets to this one view.
I have tried each one among above answers, but none of them best fits for me,
then i have looked into one of the native provided method, and it is working fine.
first, make cellSelectionStyle to None and then go for this solution.
func tableView(_ tableView: UITableView, willDeselectRowAt indexPath: IndexPath) -> IndexPath?
{
let cell = tableView.cellForRow(at: indexPath);
//cell which is getting deselected, make whatever changes that are required to make it back normal
cell.backgroundColor = kNormalColor;
return indexPath;
}
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath?
{
let cell = tableView.cellForRow(at: indexPath);
//cell which is getting selected, make whatever changes that are required to make it selected
cell.backgroundColor = kSelectedColor;
return indexPath;
}
advantage of this methods over other all is :
I have a highly customized UITableViewCell. So I implemented my own cell selection.
cell.selectionStyle = UITableViewCellSelectionStyleNone;
I created a method in my cell's class:
- (void)highlightCell:(BOOL)highlight
{
if (highlight) {
self.contentView.backgroundColor = RGB(0x355881);
_bodyLabel.textColor = RGB(0xffffff);
_fromLabel.textColor = RGB(0xffffff);
_subjectLabel.textColor = RGB(0xffffff);
_dateLabel.textColor = RGB(0xffffff);
}
else {
self.contentView.backgroundColor = RGB(0xf7f7f7);;
_bodyLabel.textColor = RGB(0xaaaaaa);
_fromLabel.textColor = [UIColor blackColor];
_subjectLabel.textColor = [UIColor blackColor];
_dateLabel.textColor = RGB(0x496487);
}
}
In my UITableViewController class in ViewWillAppear added this:
NSIndexPath *tableSelection = [self.tableView indexPathForSelectedRow];
SideSwipeTableViewCell *cell = (SideSwipeTableViewCell*)[self.tableView cellForRowAtIndexPath:tableSelection];
[cell highlightCell:NO];
In didSelectRow added this:
SideSwipeTableViewCell *cell = (SideSwipeTableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];
[cell highlightCell:YES];
Set selection property to None, make sure tableView has 'Single Selection' set and use this method in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
delegate method:
extension UITableViewCell {
func setSelectionColor(isSelected: Bool, selectionColor: UIColor, nonSelectionColor: UIColor) {
contentView.backgroundColor = isSelected ? selectionColor : nonSelectionColor
}
}
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];
}
}
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];