【发布时间】:2016-07-16 11:10:54
【问题描述】:
我正在 UITableViewCell 中创建一个表单,该表单在原型单元格中具有标签和文本字段。在此处查看其图像 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 个字段,当我在字段中输入值并滚动视图以填充剩余字段时,字段中的值会发生变化。 请帮忙。
【问题讨论】:
-
您需要在数据源中跟踪哪个文本字段有文本。因此,当
tableView:(tableView:cellForRowAtIndexPath:)中有一个时,你放它,否则,你放你的 placeHolder,因为单元格被重复使用。 -
从不使用包含表格视图单元格的 UI 元素的数组 (
arrayTextField)。这可能是你的问题的原因。考虑改进(数据源)模型的设计,更改模型中的数据并重新加载表格视图。 -
跟踪数据源我正在使用
arrayTextField@Larme 如何在数据源中跟踪以查看哪个文本字段有文本。我已经搜索过,但一无所获。 -
文本字段数组是对模型-视图-控制器模式的滥用。这会导致您的问题。 不要那样做。您应该跟踪模型中的相应属性,该属性在
cellForRowAtIndexPath中设置文本字段的文本。
标签: ios iphone swift uitableview xcode6