簡體   English   中英

在Swift中以編程方式突出顯示UITableViewCell

[英]Programmatically highlighting UITableViewCell in Swift

如何以編程方式突出顯示表格視圖單元格?

我在用:

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

我遇到的問題是正在選中該單元格(正在執行didSelectRowAt中指定的所有操作),但是該單元格現在突出顯示了。 它似乎沒有被選中。
我想要達到的目標就像iPad上的“設置”應用程序。 您啟動該應用程序,然后看到“常規”單元格已被選中並突出顯示。
當用戶觸摸時,單元格會正確突出顯示。

我什至嘗試覆蓋UITableViewCell的子類中的isSelected變量。

好的,事實證明這是后台任務的問題。 在我選擇它們之后,再次加載了這些單元格,這就是為什么選擇不可見的原因。

嘗試這個:-

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

注意 :您的問題標題說,您有一個帶有單元格突出顯示的查詢,而您的問題說明說,您在選擇單元格時遇到了問題。 兩者都是不同的事件。

這是一個答案,您如何在表格視圖行(單元格)選擇上進行管理(設置顏色):

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

這是處理單元格背景選擇的另一個選項

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM