【发布时间】:2016-06-28 08:45:59
【问题描述】:
我想在表格视图的特定单元格中插入文本字段。我有一个包含 12 个字符串的数组(其中 3 个是空格)。表格视图中的那些空白单元格,我想在其中创建一个文本字段,以便用户可以输入那些空槽。但是我无法仅在那些空白的插槽中创建文本字段。我该怎么做?
var items = ["Apple", "Fish", "Dates", "Cereal", "Ice cream", "Lamb", "Potatoes", "Chicken", "Bread", " ", " "," "]
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellFruit", forIndexPath: indexPath)
if (items[indexPath.row] == " ") {
cell.textLabel?.text = items[indexPath.row]
let collar = UIColor.init(red: 175, green: 189, blue: 212, alpha: 0.4)
cell.backgroundColor = collar
return cell
}
else {
cell.textLabel?.text = items[indexPath.row]
let coL = UIColor.init(red: 59, green: 89, blue: 152, alpha: 0.2)
cell.backgroundColor = coL
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()
}
【问题讨论】:
-
到目前为止你有什么?请粘贴一些代码..
-
先获取数组值,检查值是否为空。如果它为空,则将您的文本字段添加到单元格。否则添加字符串值。
-
@user1941284 我刚刚发布了代码。 items 是我用来创建行的数组的名称。
-
@Signare 我知道:P 事情是我不知道如何在 if 语句后数组为空时在单元格中添加文本字段
-
在 if 循环中,添加这一行。 cell.contentView.addSubview(YourTextField)
标签: ios swift uitableview uitextfield