ios - UITableViewCell 滚动后数据发生变化

标签 ios iphone swift uitableview xcode6

我正在 UITableViewCell 中创建一个表单,该表单在原型(prototype)单元格中具有标签和文本字段。在这里查看它的图像 TableView Story board Image 。 我正在使用标识符动态创建表单,我的代码是

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("protocell", forIndexPath: indexPath) as! UITableViewCell

    var lable = cell.viewWithTag(1) as! UILabel
    lable.text = details[indexPath.row]
    lable.textColor = UIColor.brownColor().colorWithAlphaComponent(0.7)

    var textfield = cell.viewWithTag(2) as! UITextField
    textfield.placeholder = "Enter \(details[indexPath.row])"
    self.arrayTextField.append(textfield)
    if details[indexPath.row] == "Name" {
        textfield.placeholder = "Enter First \(details[indexPath.row]) Middle \(details[indexPath.row]) Last \(details[indexPath.row]) "
    }
    textfield.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.2)

    return cell
}

现在的问题是我有 18 个字段,当我在字段中输入值并 ScrollView 以填充剩余字段时,字段中的值会发生变化。 请帮忙。

最佳答案

创建 UITableViewCell 子类并重写 prepeareForReuse 函数 - 将单元格转换为默认模式。

swift :

override func prepareForReuse() {
    super.prepareForReuse()

    //set cell to initial state here, reset or set values, etc.
}

根据您的评论 - 如何子类化 UITableViewCell:

import UIKit

class MyCustomCell: UITableViewCell {
     // things that you can do here:
     // initialise your properties here. (label, textfield. etc)
     // layout subviews.
     // override superclass APIs.
}

关于ios - UITableViewCell 滚动后数据发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38410582/

相关文章:

ios - 从 Collection View Controller 调用时, Collection View header 中的 UIButton 不工作,如何解决?

ios - 在 Xcode 4.2 中使用 Reachability.m

iphone - 如何以编程方式更改 iPhone 设置

swift - 注册后如何/何时删除 Firebase Auth 监听器并写入数据库?

Swift OptionSetType 重置自定义属性

ios - 禁用特定选项卡栏项目排序的最佳方法

ios - UndoManager运行循环分组将如何在不同的线程上下文中受到影响?

iphone - drawRect 在 iPhone(模拟器)中反转 imageView

iphone - 将 c-struct 放入 NSArray 的最佳方法是什么?

iphone - UIWindowController 断言失败