Custom UI Tableviewcell Selected Backgroundcolor Swift

Custom UI TableViewCell selected backgroundcolor swift

You've the right method already in there: didSelectRowAtIndexPath. In that method you can call tableView.cellForRowAtIndexPath(indexPath) and get your cell. Than you can set the cell-background to your color:

 func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("Row \(indexPath.row) selected")
let cell:YourCustomCell = tableView.cellForRowAtIndexPath(indexPath) as YourCell
cell.backgroundColor = UIColor.redColor()
}

Or, a better way would be to check in your cellForRowAtIndexPath method, if a cell is selected:

if(cell.selected){
cell.backgroundColor = UIColor.redColor()
}else{
cell.backgroundColor = UIColor.clearColor()
}

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
}
}

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 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 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

Change background color of selected UITableViewCell

Add this code in cellForRowAtIndexPath. Thanks to @danh, added check if there's already selectedBackroundView so no need to allocate new one.

if cell.selectedBackgroundView == nil{
cell.selectedBackroundView = UIView()
}
cell.selectedBackgroundView.backgroundColor = // your color

custom tableview cell selection color in custom cell

Finnaly i have found the answer to my question..

I've made very stupid mistake..
So first thing you need to set the files owner class as NSObject to your customcell in IB,
then your uitableviewcell class as your custom cell, in my case "myCustomDisplayCell"..

Futhermore from bottom to top (views in IB), I've added a uiview and hooked up with cells backgroundview,
on top i've placed a uiview and hooked up with cells selectedbackgroundview(made view color clear)
then finnaly i placed a uilabel uiimageview, and hooked up these right.. (you can add your own things here needed for custom cells). And that's it! Looks kind of easy now..

Thank you all for help!



Related Topics



Leave a reply



Submit