簡體   English   中英

去除 UITableView 的單元格高亮顏色

[英]Remove the cell highlight color of UITableView

我想刪除 uitableview 單元格選擇的默認藍色。 我不想要任何選擇顏色。 我還沒有創建自定義單元格類。 我通過在單元格上添加標簽和按鈕來自定義單元格。 我試着做:

cell.selectioncolor = [UIColor clearcolor];

但它說這種方法已被棄用。

cell.selectionStyle = UITableViewCellSelectionStyleNone;

Swift 4 中更新

cell.selectionStyle = UITableViewCell.SelectionStyle.none

或者

cell.selectionStyle = .none

StoryboardXIB選擇此項


在此處輸入圖片說明

// Swift 2.0

cell.selectionStyle = UITableViewCellSelectionStyle.None

斯威夫特 3.0

cell.selectionStyle = .none

目標-C:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

或者

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

斯威夫特 4:

self.selectionStyle = UITableViewCellSelectionStyle.none;

斯威夫特 3:

cell.selectionStyle = .none

斯威夫特 2:

cell.selectionStyle = UITableViewCellSelectionStyle.None

如果您只想使用 Storyboard/Xib 更改它,只需選擇要刪除“選擇樣式效果”的單元格並將其定義為“無”。 它也會像魔術一樣工作:D

故事板 / Xib

將 TableView Selection 樣式設置為.none會影響我的應用程序中 tableview 的響應能力和性能( didSelectRowAt indexPath水龍頭被延遲)。 我對這個問題的解決方案是在第一次創建單元格時隱藏awakeFromNib()上的選定背景視圖:

selectedBackgroundView?.isHidden = true

試試這個快速

cell?.selectionStyle = UITableViewCellSelectionStyle.None

在單元格中執行此操作:

class YourCell:  UITableViewCell {
    
    override func didMoveToSuperview() {
        selectionStyle = .none
    }

    ...
}

就這么簡單。

正確答案應該是:

cell.selectedBackgroundView?.backgroundColor = <choose your color>

選擇類型是一個不同的屬性,當設置為.none會產生你想要的以及其他不需要的副作用。

如果您不想突出顯示單元格,則使其背景視圖的顏色與突出顯示時相同。

Swift 5.4 ,只需放置selectionStyle = .none

例子:

class TableViewCell: UITableViewCell {

 override func awakeFromNib() {
    super.awakeFromNib()
    
    selectionStyle = .none 

}

迅捷 5

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        cell.selectionStyle = .none

        return cell
    }  

暫無
暫無

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

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