How to prevent UITableView from reuse custom cells Swift

前端 未结 4 1946
醉梦人生
醉梦人生 2021-01-02 05:43

In my App I use UITableView with custom cells.

for each cell I implement function to create it, and call these functions in cellFo

相关标签:
4条回答
  • 2021-01-02 05:54

    Don't use the default UITableView and fight the reuse of cells, it's really embedded in it's behaviour.

    Try to adapt your code so it works well with reusing cells, or if that's really impossible, you'd have to write your own custom table view I guess (but I don't recommend that at all)

    0 讨论(0)
  • 2021-01-02 06:04

    Try using a different identifier for each of the cell types, so dont use "text-field-cell" for each one, make one "full name", "password" etc. not sure how you are going about creating your cells, but if you are using the registerNib or registerClass, you will need to register it for each different identifier

    self.tableView.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "full name")
    self.tableView.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "password")
    self.tableView.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "email")
    
    0 讨论(0)
  • 2021-01-02 06:09

    You shouldn't prevent that, but if you really want to, I think that not setting cell identifier to your UITableViewCell should do the trick. When you'll call dequeueReusableCellWithIdentifier, it will find no cell and will create a new one. But again, it's not recommended as UITableView is made to be used with reuse ability.

    Moreover, as @Fonix suggests, using a different cell identifier for each cell of your UITableView might solve your issue while keeping using cell reuse system.

    Edit : As you're talking about losing text content in your UITextField, maybe the single change to make is changing your @propery weak attribute to strong.

    Hope this helps.

    0 讨论(0)
  • 2021-01-02 06:17

    Try to implement prepareForReuse method in your custom cell and set all fields text to nil.

    override func prepareForReuse() -> Void {
       awamirTextFieldNib.text = nil
    }
    

    Hope this help

    0 讨论(0)
提交回复
热议问题