我正在 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/