单击自定义单元格的 UITextField 后重新加载 TableView

Reload TableView after clicking on a custom cell's UITextField

我有一个带有自定义单元格的 tableView。每个自定义单元格都有一个文本字段。当用户单击文本字段时,我希望某些新单元格出现在屏幕上。

这就是我正在尝试的:

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    /*
    ...code to determine which new cells should be added in from my data structure
    */
    self.tableView.reloadData()
    return true
}

但是,每次调用 reloadData 时,我认为委托会再次调用 textFieldShouldBeginEditing,所以我陷入了无限循环并且该方法永远不会 returns。

关于如何避免这个问题有什么想法吗?

我认为您正在寻找的是 insertRowsAtIndexPaths 调用,而不是重新加载整个 table。

  func insertRowsAtIndexPaths(_ indexPaths: [NSIndexPath], withRowAnimation animation: UITableViewRowAnimation)

另一个堆栈溢出post you should look at

这是 link 到 Apple docs

请记住,在执行此操作时,您还需要使数据源保持最新。在调用 insertRowsAtIndexPath

之前更新数据源