简体   繁体   中英

Programmatically highlighting UITableViewCell in Swift

How do I programmatically highlight a table view cell?

I am using:

tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
tableView.delegate?.tableView!(self.ordersTableView, didSelectRowAt: indexPath)

The problem I am having is that the cell IS being selected (all the actions specified in my didSelectRowAt are being performed) but the cell does now highlight. It doesn't appear selected.
What I am trying to achieve is like the Settings app on the iPad. You launch the app and see the "general" cell already selected and highlighted.
The cells highlight properly when touched by the user.

I have even tried overwriting the isSelected variable in my subclass of UITableViewCell.

Ok turns out it was a problem with background tasks. The cells were loaded again after I selected them and that's why the selection wasn't visible.

Try this:-

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
var selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
selectedCell.contentView.backgroundColor = UIColor.redColor()
}

Note : Your question title says, you have a query with cell highlight and your question description says, you have a problem with cell selection. Both are different events.

Here is an answer, how you can manage (set color) on tableview row (cell) selection:

// make sure your tableview row selection is enabled
yourTableViewInstance.allowsSelection = true


// handle highlighted background in data source method also
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "textview") as! YourTableViewCell
    cell.contentView.backgroundColor = cell.isSelected ? UIColor.red : UIColor.gray
    return cell
}



// didSelectRowAtIndexPath - change background color
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) as? YourTableViewCell {
        cell.contentView.backgroundColor = UIColor.red
    }
}


// didDeselectRowAtIndexPath - change background color
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) as? YourTableViewCell {
        cell.contentView.backgroundColor = UIColor.gray
    }
}

Here is another option to handle cell background selection

class YourTableViewCell: UITableViewCell {

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

      if self.selectedBackgroundView == nil {
        let bgView = UIView()
        self.selectedBackgroundView = bgView
      }
      self.selectedBackgroundView?.backgroundColor = selected ? UIColor.red : UIColor.gray
    }


}


// enable row/cell selection
yourTableViewInstance.allowsSelection = true


// handle highlighted background in data source method also
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "textview") as! YourTableViewCell

     if self.selectedBackgroundView == nil {
        let bgView = UIView()
        self.selectedBackgroundView = bgView
    }
    self.selectedBackgroundView?.backgroundColor = selected ? UIColor.red : UIColor.gray

    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