【发布时间】:2016-06-21 08:05:35
【问题描述】:
在我的应用中,我使用 UITableView 和 custom 单元格。
对于每个单元格,我实现了创建它的函数,并在 cellForRow 中调用这些函数。
这是来自项目的代码示例:
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()
}}
这些是创建函数:
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
}
问题是如果我重新加载表格视图,单元格的内容会因为单元格的可重用性而改变。即:重新加载tableview后,第一个单元格的内容变成了第二个单元格,第二个单元格的内容变成了第一个单元格!!
如何防止 tableview 重复使用单元格?!
谢谢。
【问题讨论】:
-
你不应该阻止这一点,你应该高兴
UITableView有这个能力=) -
你不应该阻止细胞重复使用......相反,你应该学会重复使用你的细胞,否则你会做很多不必要的处理并使你的应用程序电池效率低下
-
你的代码对我来说看起来有点乱,你确实重用了单元格,但在将重用单元格的内容设置为 normal 之前,你如何重置重用单元格的内容并不明显、pawwsord 等...
标签: ios swift uitableview