簡體   English   中英

如何創建不可重用的 UITableViewCell

[英]How to create a non-reusable UITableViewCell

編輯:似乎我對“小/固定數量的單元格”的含義並不完全清楚。 我的意思是細胞的數量是不變的(總是只有 10 個細胞)。 但是,單元格的內容是非常動態的。 我有一個復雜的 UITableViewCell class 來處理這個問題,我很難在給定的時間“捕獲”每個元素和變量的確切 state 以使“重用”正常工作。

我知道它非常不受歡迎......但我想創建不可重用的 UITableViewCells。 我決定這樣做的原因是因為我只需要少量/固定數量的單元格(准確地說是 10 個),而且它們非常復雜,所以我覺得必須在重用它們之前捕獲它們的各種狀態,然后重新設置顯示每個單元格時的這些狀態會非常乏味,特別是因為 memory 似乎不會成為只有十個不可重復使用的單元格的問題。

但是,我很困惑如何做到這一點(實現這一非正統任務的在線資源非常有限)。 作為一個簡化的測試,我嘗試在cellForRowAt數據源方法中簡單地創建一個 CustomTableViewCell 的實例,但該單元格顯示為完全空白。 在顯示單元格之前似乎沒有建立任何 IBOutlets(我在單元格的awakeFromNib()方法中發布了print()語句,並且從未調用過它)。

有誰知道我遺漏了哪些步驟和/或做錯了什么?

作為參考,這是我的表格視圖數據源的cellForRowAt方法:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  
  // Assuming for the sake of the argument that there is only one row (so only one cell needed)...  
  let cell = CustomTableViewCell()
  return cell

}

我也嘗試使用let cell = CustomTableViewCell(style: .default, reuseIdentifier: "customCell")創建單元格,但這也不起作用。

任何幫助將不勝感激 - 謝謝!

為了使您的單元格 static,一旦您必須為屬性檢查器創建 tableViewController go,則將內容類型從動態變為 static。 所以你可以將單元格的數量固定為特定的數量(在下面的第一張和第二張圖片中提到)..

在此處輸入圖像描述

在此處輸入圖像描述

不確定這是否回答了您的問題,但您可以嘗試在您的CustomTableViewCell()中調用覆蓋函數 prepareForReuse( ) 。 例如,如果您想在表格視圖中添加一個開關,以避免可重用單元格將列表中的所有開關弄亂,您可以這樣做:

override func prepareForReuse() {
    super.prepareForReuse()
    // declare the switch with the default state
    mySwitch.isOn = false
 
}

一點澄清...

僅僅因為您使用可重復使用的單元格並不意味着您必須對其執行任何“動態”操作。

假設你在 Storyboard 中設計你的細胞,你可能會有這樣的事情:

在此處輸入圖像描述

我設計了 5 個“復雜靜態”單元。 它們看起來正是我希望它們顯示的樣子,因此沒有自定義單元類或@IBOutlet連接。

我所做的只是給每個人一個不同的identifier ……在這種情況下,我使用了“type1”/“type2”/“type3”/“type4”/“type5”。

現在,一個簡單的 controller class:

class RowTypesTableViewController: UITableViewController {
    
    var theData: [Int] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()

        theData = [1, 2, 3, 4, 5]

    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return theData.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let id: String = "type\(theData[indexPath.row])"
        let cell = tableView.dequeueReusableCell(withIdentifier: id, for: indexPath)
        return cell
    }
    
}

請注意,在cellForRowAt中,我使用數據源值來生成重用標識符:

let id: String = "type\(theData[indexPath.row])"

它加載(出列)正確的“靜態”單元格,我得到這個 output:

在此處輸入圖像描述

如果我將數據源數組更改為:

theData = [1, 3, 4]

我明白了:

在此處輸入圖像描述

使用theData = [5, 4]

在此處輸入圖像描述

並使用theData = [4, 3, 5, 1, 2]

在此處輸入圖像描述

如您所見,單元格是“靜態的”,但我只能按我想要的順序顯示我想要的單元格。

如果您在xib文件中設計單元格,請將其添加到viewDidLoad()

    tableView.register(UINib(nibName: "type1", bundle: nil), forCellReuseIdentifier: "type1")
    tableView.register(UINib(nibName: "type2", bundle: nil), forCellReuseIdentifier: "type2")
    tableView.register(UINib(nibName: "type3", bundle: nil), forCellReuseIdentifier: "type3")
    tableView.register(UINib(nibName: "type4", bundle: nil), forCellReuseIdentifier: "type4")
    tableView.register(UINib(nibName: "type5", bundle: nil), forCellReuseIdentifier: "type5")
    

顯然,將“類型”字符串替換為對您的代碼有意義的字符串。

暫無
暫無

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

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