Uitableview Cell Selected Color

UITableView Cell selected Color?

I think you were on the right track, but according to the class definition for selectedBackgroundView:

The default is nil for cells in plain-style tables (UITableViewStylePlain) and non-nil for section-group tables UITableViewStyleGrouped).

Therefore, if you're using a plain-style table, then you'll need to alloc-init a new UIView having your desired background colour and then assign it to selectedBackgroundView.

Alternatively, you could use:

cell.selectionStyle = UITableViewCellSelectionStyleGray;

if all you wanted was a gray background when the cell is selected. Hope this helps.

UITableViewCell Selected Background Color on Multiple Selection

Swift 4.2

For multiple selections you need to set the UITableView property allowsMultipleSelection to true.

myTableView.allowsMultipleSelection = true

In case you subclassed the UITableViewCell, you override setSelected(_ selected: Bool, animated: Bool) method in your custom cell class.

 override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

if selected {
contentView.backgroundColor = UIColor.green
} else {
contentView.backgroundColor = UIColor.blue
}
}

How to change selected color for table view cell?

You can do either one of this -

  1. Change the selectionStyle property of your cell.

For example: If you change it to UITableViewCellSelectionStyleGray, it will be gray.

 cell.selectionStyle = UITableViewCellSelectionStyleGray;

  1. Change the selectedBackgroundView property.

    let bgColorView = UIView()
    bgColorView.backgroundColor = UIColor.red
    cell.selectedBackgroundView = bgColorView

swift 4 correction:

    cell.selectionStyle = UITableViewCellSelectionStyle.gray

UITableViewCell Selected Background Color on DidSelectRow

Do not change the backgroundColor of the contentView or of the cell itself, use the selectedBackgroundView instead:

 cell.backgroundView = UIView()
cell.backgroundView?.backgroundColor = // your color when not selected

cell.selectedBackgroundView = UIView()
cell.selectedBackgroundView?.backgroundColor = // your color when selected

After this initial setup, there is no need to change anything. Everything will work automatically.

Do not set cell.selectionStyle = .none because that will disable selection completely.

How to change the blue highlight color of a UITableViewCell?

You can change the highlight color in several ways.

  1. Change the selectionStyle property of your cell. If you change it to UITableViewCellSelectionStyleGray, it will be gray.

  2. Change the selectedBackgroundView property. Actually what creates the blue gradient is a view. You can create a view and draw what ever you like, and use the view as the background of your table view cells.

Change UITableViewCell selection color based on which cell was pressed

I assume you have created custom UITableviewCell. Create a cell type enum.

 enum CellType {
case RedCell
case Yellowcell
case OrangeCell
}
//Create enum property
class CustomCell : UITableViewCell {
var cellType:CellType = CellType.RedCell //Default is RedCell
}

Now you have to assign the cell type in your ViewController tableview datasource.

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!  {
var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCell
cell.cellType = .RedCell //your choice
return cell
}

override func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}

override func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {
var cell = tableView.cellForRowAtIndexPath(indexPath)
switch(cell.cellType) {
//Handle Switch case
case .RedCell:
cell?.contentView.backgroundColor = UIColor.redColor()
cell?.backgroundColor = UIColor.redColor()

}

}

override func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) {
var cell = tableView.cellForRowAtIndexPath(indexPath)
// Set unhighlighted color
cell?.contentView.backgroundColor = UIColor.blackColor()
cell?.backgroundColor = UIColor.blackColor()
}

EDIT:
If you have created 3 different types of cell class check tableview cell class type and change the color in didHighlightRowAtIndexPath method.

UITableView: Change selected cell background color

tvOS use focus to show the current selected item. To change the backgroundColor, override the didUpdateFocusInContext() method in your cell class.

public override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
context.previouslyFocusedView?.backgroundColor = UIColor.clearColor()
context.nextFocusedView?.backgroundColor = UIColor.blackColor()
}

UITableView Static cells - Selection color not working

Add the following code to your TableView: cellForRowAtIndexPath method.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

// Write code for create your cell and properties regarding that cell
//add this line
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor blueColor];
[cell setSelectedBackgroundView:bgColorView];

return cell;
}

Remove the cell highlight color of UITableView

In Swift:

cell.selectionStyle = UITableViewCell.SelectionStyle.none

or simply:

cell.selectionStyle = .none


Related Topics



Leave a reply



Submit