【发布时间】:2016-06-28 12:46:48
【问题描述】:
我正在开发这个应用程序,而且我是 Swift 新手。只有两周的知识。我应该创建一个包含 12 个单元格的表格视图 - 其中 3 个应该是文本字段,用户可以输入他们想要的内容。我制作了两个具有两个不同标识符的原型单元。我正在使用一个名为“items”的数组,其中包含要在单元格中表示的字符串。如果字符串为空白,则该单元格应该是一个文本字段,我已经这样做了。之后,当我尝试输入这些字段并滚动单元格时,就会出现问题。
请帮助我理解和解决以下问题:
- 如何将作为子视图添加的文本字段委托给我的 tableview 单元格?
- 如何确保我在文本字段中输入的任何内容都保留在那里,即使在我根据需要上下滚动单元格之后也是如此?
- 如何确保用户可以编辑他们在文本字段中键入的任何内容?
这是我的代码:
var items = ["Apple", "Fish", "Dates", "Cereal", "Ice cream", "Lamb", "Potatoes", "Chicken", "Bread", " ", " "," "]
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var iD = "normal"
if (items[indexPath.row] == " ") {
iD = "textField"
let cell = tableView.dequeueReusableCellWithIdentifier(iD, forIndexPath: indexPath)
cell.textLabel?.text = items[indexPath.row]
let textField = UITextField(frame: CGRect(x: 20, y: 10, width: 150, height: 30))
textField.backgroundColor = UIColor.brownColor()
textField.placeholder = ""
cell.contentView.addSubview(textField)
return cell
}
else {
let cell = tableView.dequeueReusableCellWithIdentifier(iD, forIndexPath: indexPath)
cell.textLabel?.text = items[indexPath.row]
return cell
}
////
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
if (sourceIndexPath.row != destinationIndexPath.row){
let temp = items[sourceIndexPath.row]
items.removeAtIndex(sourceIndexPath.row)
items.insert(temp, atIndex: destinationIndexPath.row)
}
tableView.reloadData()
}
【问题讨论】:
-
您好,欢迎来到 StackOverflow。请在每个帖子中保留一个问题,否则
a)将难以创建答案,b)您将难以选择“正确”答案。
标签: ios swift uitableview uitextfield