簡體   English   中英

在Swift中的UITableView中保留單元格?

[英]Retain cells in UITableView in Swift?

UITableView默認情況下重用單元格。 但是在我的情況下,某些單元格具有UITextField對象,並且當單元重用時,我不想丟失其值。 該表有幾行,因此我可以處理內存中的所有單元。

我當前使用的代碼示例:

override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UINib(nibName: "TPAddressInfoTableViewCell", bundle: nil), forCellReuseIdentifier: InfoCellIdentifier)
        ...

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let item = items[indexPath.row]
        switch item.appearance {
        case .textWithTitle:
            let cell = tableView.dequeueReusableCell(withIdentifier: InfoCellIdentifier) as! TPAddressInfoTableViewCell
            return cell
        case .picker:
            ...

如何正確保留此類細胞? 在這種情況下,將每個單元格的UITextFieldDelegate鏈接到當前的UIViewController似乎是多余的。

不要保留細胞

將單元格的UI元素的所有值保存在數據模型中,並在cellForRow還原這些值。

例如,將相應的模型項傳遞到cellForRow的單元格,並在文本字段字符串更改時將其保存在屬性中。
或者,使用回調閉包來更新控制器中的屬性。

暫無
暫無

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

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