我是新手。我有一个UITableView与一个自定义UITableViewCell。它有一个UITextfield。默认情况下,numberOfRows为1。
当我在该文本字段中输入整数值(x)并按Enter键时,tableview应附加x个tableviewcells,每个单元格应该有一个textfield。
这是我的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : TableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! TableViewCell
cell.textField?.text = "Created"
cell.textField.tag = indexPath.row
cell.textField.addTarget(self, action: #selector(textFieldShouldReturn()), forControlEvents: <#T##UIControlEvents#>)
return cell
}
答案 0 :(得分:0)
要实现您想要的功能,请在用户完成编辑时设置IBAction以检查插入的值是否为有效数字,如果它是增加数据源上的记录数并调用tableView.reloadData()来刷新表。
在你的cellForRowAtIndexPath上你可以对这行进行验证,以防你想要与第一个不同的单元格
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell!
if indexPath.row == 0 {
cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
cell.textField?.text = "Created"
cell.textField.tag = indexPath.row
UITextField().addTarget(self, action: #selector(customMethod), for: .editingDidEnd)
} else {
cell = self.tableView.dequeueReusableCellWithIdentifier("otherCell") as! UITableViewCell
cell.textLabel?.text = "Created programatically"
}
return cell
}