How to prevent UITableView from reusing custom Swift cells

In my application, I use UITableView with custom cells.

for each cell, I implement a function to create it and call these functions in cellForRow.

This is an example of code from a project:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if indexPath.row == CellsTypes.profileImageCell.rawValue { return createIntroCell() } else if indexPath.row == CellsTypes.fullNameCell.rawValue { return createUserNameCell() } else if indexPath.row == CellsTypes.emailCell.rawValue { return createEmailCell() } else { return createPasswordCell() }} 

and these are the creation functions:

 func createUserNameCell() -> TextFieldTableViewCell { let fullNameCell = self.tableView.dequeueReusableCellWithIdentifier("text-field-cell") as! TextFieldTableViewCell fullNameCell.awamirTextFieldNib.setNormalTextFieldCellContent("Full Name") fullNameCell.awamirTextFieldNib.textFieldContent.tag = CellsTypes.fullNameCell.rawValue fullNameCell.awamirTextFieldNib.textFieldContent.returnKeyType = .Next fullNameCell.awamirTextFieldNib.textFieldContent.delegate = self return fullNameCell } func createEmailCell() -> TextFieldTableViewCell { let emailCell = self.tableView.dequeueReusableCellWithIdentifier("text-field-cell") as! TextFieldTableViewCell emailCell.awamirTextFieldNib.setNormalTextFieldCellContent("Email") emailCell.awamirTextFieldNib.textFieldContent.tag = CellsTypes.emailCell.rawValue emailCell.awamirTextFieldNib.textFieldContent.returnKeyType = .Next emailCell.awamirTextFieldNib.textFieldContent.delegate = self return emailCell } func createPasswordCell() -> TextFieldTableViewCell { let textFieldCell = self.tableView.dequeueReusableCellWithIdentifier("text-field-cell") as! TextFieldTableViewCell textFieldCell.awamirTextFieldNib.setPasswordCellContent("Password") textFieldCell.awamirTextFieldNib.textFieldContent.tag = CellsTypes.passwordCell.rawValue textFieldCell.awamirTextFieldNib.textFieldContent.returnKeyType = .Next textFieldCell.awamirTextFieldNib.textFieldContent.delegate = self return textFieldCell } 

the problem is that if I reload the table, the contents of the cells have changed due to the reuse of the cells. ie: after restarting the tableview, the contents of the first cell will become in the second cell, and the contents of sencond on will become in the first cell !!

How can I prevent the display of a table view in cells ?!

Thanks.

+5
source share
4 answers

Try using a different identifier for each of the cell types, so do not use a "text-field-cell" for each of them, make one "full name", "password", etc., don’t know how you are going to create your cells but if you use registerNib or registerClass you need to register it for every other 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") 
+6
source

Try implementing the prepareForReuse method in your user cell and set the text to zero for all fields.

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

Hope for this help

+4
source

You should not interfere with this, but if you really want to, I think that not setting the cell identifier in your UITableViewCell should do the trick. When you call dequeueReusableCellWithIdentifier , it will not find the cell and create a new one. But then again, this is not recommended, as the UITableView used for reusable use.

Also, as @Fonix suggests, using a different cell identifier for each cell of your UITableView can solve your problem, while preserving the use of the reuse system.

Edit: Since you are talking about losing text content in your UITextField, perhaps the one change you need to change changes your @propery weak attribute to strong .

Hope this helps.

+1
source

Do not use the default UITableView and fight against cell reuse, it really is built into its behavior.

Try to adapt your code so that it works well with cell reuse, or if it is really impossible, you will have to write your own kind of table, I think (but I do not recommend this at all)

0
source

All Articles