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

时间:2016-02-03 01:46:24

标签: swift uitableview delegates infinite-loop reloaddata

我有一个带自定义单元格的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,因此我陷入无限循环,方法永远不会返回。

关于如何避免此问题的任何想法?

1 个答案:

答案 0 :(得分:0)

I think what you are looking for is the insertRowsAtIndexPaths call instead of reloading the whole table.

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

another stack overflow post you should look at

here's the link to the Apple docs

keep in mind you when doing this you need to keep your datasource up to date as well. update the datasource before calling insertRowsAtIndexPath