如何按钮突出显示和突出显示颜色迅速变红

时间:2018-12-04 05:35:55

标签: swift uitableview

如何显示按钮高亮显示颜色,并且颜色为红色

extension LeaveDetailVC: cellIndexCall{
    func selectBtnIndex(sender: UIButton) {
        let buttonPosition:CGPoint = sender.convert(.zero, to:leaveDetailTableView)
        var indexPath = leaveDetailTableView.indexPathForRow(at: buttonPosition)
        self.indexPath = indexPath!
        print("\(String(describing: indexPath?.row))") /* index path of button
        self.menuClickIndex = (indexPath?.row)!   
    }
}

我的Button覆盖了tableViewCell。 而且,我还在单元格类中创建了一个按钮委托,并通过扩展名调用viewController。

我只想突出显示按钮,它位于单元格上方。

2 个答案:

答案 0 :(得分:0)

  override func tableView(_ tableView: UITableView, shouldHighlightRowAt 
  indexPath: IndexPath) -> Bool {
    return true
   }

  override func tableView(_ tableView: UITableView, didHighlightRowAt 
  indexPath: 
   IndexPath) {
   let cell = tableView.cellForRow(at: indexPath)
   cell?.contentView.backgroundColor = UIColor.orange
   cell?.backgroundColor = UIColor.orange
 }

  override func tableView(_ tableView: UITableView, didUnhighlightRowAt 
  indexPath: IndexPath) {
     let cell = tableView.cellForRow(at: indexPath)
     cell?.contentView.backgroundColor = UIColor.black
     cell?.backgroundColor = UIColor.black
   }

答案 1 :(得分:0)

在您的viewController中

class LeaveDetailVC: ViewController {
    var selectedRows: [IndexPath] = [] // i assume multiple select button

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "YOUR_CELL") as! YOUR_CELL_CLASS
          cell.isHighlight = selectedRows.indices.contains(indexPath) // Determine if cell was selected or not
    }
}

extension LeaveDetailVC: cellIndexCall {

    func selectBtnIndex(sender: UIButton) {
       let buttonPosition:CGPoint = sender.convert(.zero, to:leaveDetailTableView)
       var indexPath = leaveDetailTableView.indexPathForRow(at: buttonPosition)
       if selectedRows.indices.contains(indexPath) {
          if let index = selectedRows.index(of: indexPath) {
             self.selectedRows.remove(at: index) // remove selected indexpath
          }
       } else {
          self.selectedRows.append(indexPath) // add selected indexpath
       }
    }
}

在您的单元格班级

var isHighlight: Bool = false

override func layoutSubviews() {
   super.layoutSubviews()

   // TODO : Set your button color based on isHighlight flag
}