如何防止UITableView重用自定义单元Swift [英] How to prevent UITableView from reuse custom cells Swift

查看:99
本文介绍了如何防止UITableView重用自定义单元Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我使用 UITableView 自定义单元格。

In my App I use UITableView with custom cells.

为每个单元格实现创建它的函数,并在cellForRow中调用这些函数。

for each cell I implement function to create it, and call these functions in 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后,第一个单元格的内容变为第二个单元格,并且sencond的内容变为第一个单元格!!

the problem is if I reload the tableview the content of cells changed because of the reusablity of the cells. i.e: after reload the tableview the content of the first cell become in the second cell, and the content of the sencond on became in the first cell!!

我怎么能阻止tableview重复使用单元格?!

how can I prevent tableview from reusable the cells?!

谢谢。

推荐答案

尝试为每个单元格类型使用不同的标识符,因此不要为每个单元格使用text-field-cell,制作一个全名,密码等。不确定如何创建单元格,但如果使用registerNib或registerClass,则需要为每个不同的标识符注册

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")

这篇关于如何防止UITableView重用自定义单元Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆