【问题标题】:Reload TableView after clicking on a custom cell's UITextField单击自定义单元格的 UITextField 后重新加载 TableView
【发布时间】:2016-02-03 01:46:24
【问题描述】:

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

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

【问题讨论】:

    标签: swift uitableview delegates infinite-loop reloaddata


    【解决方案1】:

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

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

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

    这是Apple docs的链接

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

    【讨论】:

    • 谢谢!正是我想要的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多