简体   繁体   中英

Change UITableViewCell selection color based on which cell was pressed

My table view has some elements sorted by some types: TypeA, TypeB and TypeC.

I want that when I click on a cell with TypeA to change the selection color to Red, when I type on TypeB to change color to Blue and when pressing on TypeC to change color to Yellow.

Right now I came up with this code:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    guard let mode = dataSource.selectedObject else {
        fatalError("willDisplayCell, but no selected row?")
    }

    let type = ModeType(rawValue: mode.type)!
    let selectionColor = UIView() as UIView
    selectionColor.backgroundColor = type.color()
    cell.selectedBackgroundView = selectionColor
}

My issue with this is that willDisplayCell is called when I start my app and my data source is empty so I get a fatal error.

How can I overcome this ? Maybe using a flag to do this only when didSelectRowAtIndexPath was called.
Or is there another way to achieve what I am after ?

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.

My issue with this is that willDisplayCell is called when I start my app and my data source is empty so I get a fatal error.

tableView(_:willDisplayCell:forRowAtIndexPath:) will only be called if your data source tells the table view that there are rows to display. So the problem more likely is that your tableView(_:numberOfRowsInSection:) method is returning a number larger than zero when your data source is empty.

Also, your code looks like it expects tableView(_:willDisplayCell:forRowAtIndexPath:) to get called only for selected rows. It gets called for all displayed rows. But this method isn't necessary to affect the background color. In fact, it's rarely used in most apps. There are only a few edge cases where you need to mess with the cell just before it's displayed.

The proper way to set the selection background color is to create and assign a UIView to the cell's selectedBackgroundView property. You can do that either from the cell's subclass (preferred for complex cells) or from the tableView:cellForRowAtIndexPath: data source method:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("MyCell")
    cell!.textLabel?.text = "Kilroy was here."
    cell!.selectedBackgroundView = UIView(frame: cell!.bounds)
    cell!.selectedBackgroundView!.backgroundColor = .greenColor()
    return cell!
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM