从 textFieldDidEndEdit 调用重新加载数据后针对 UITableView 的 indexPath 不正



所以我试图模拟标准Apple代码在管理电话号码时在联系人应用程序上的工作方式。 具体来说,我正在努力从表视图中删除一行,如果它为空并且用户导航到任何其他行

我的问题是,由于表视图重新加载导致 UITextField 辞职其响应者,我需要为用户导航到的文本字段再次设置响应器

我有 UITextField 委托,正在处理通常的textFieldShouldBeginEditing , textFieldDidBeginEditing , textFieldShouldEndEditing , textFieldDidEndEditing

为了处理该功能,我的代码在 textFieldDidEndEditing 中,我由此从 tableview 数组中删除数据,并且由于 tableview 有 2 个部分,我调用:

[MyTableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];

textFieldDidBeginEditing期间,我使用以下方法保存正在编辑的文本字段的索引路径:

EditableCustomCell *textFieldCell = (EditableCustomCell *)[[textField superview] superview];
NSIndexPath *indexPath = [MyTableView indexPathForCell:(EditableCustomCell *)textFieldCell];
responderIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];

然后,我使用以下代码将正确的行 textField 设置为第一个响应者:

EditableCustomCell *customCell = (EditableCustomCell *)[MyTableView cellForRowAtIndexPath:responderIndexPath];
[customCell.editableTextField becomeFirstResponder];

一切似乎都很好,直到处理接近尾声,突然textFieldDidBeginEditing开始返回 indexPath 的第 0 节第 0 行(即使在检查标记值或包含的文本时返回文本字段的正确值(

以下是上述过程开始的日志:

- textFieldDidEndEditing started  <-- start of delete processing
- textFieldDidEndEditing - tableviewData - replacing object at index 1
    CustomTableViewController.deleteRow - delete called for indexPath section 1 ... row 1
- reloading MyTableView
- CustomTableViewController-cellForRowAtIndexPath started
    CustomTableViewController-cellForRowAtIndexPath section=1 row=0
    CustomTableViewController-cellForRowAtIndexPath ending
    CustomTableViewController-cellForRowAtIndexPath section=1 row=1
    CustomTableViewController-cellForRowAtIndexPath ending
    CustomTableViewController-cellForRowAtIndexPath section=1 row=2
    CustomTableViewController-cellForRowAtIndexPath ending
- textFieldShouldBeginEditing started
    indexPath for textFieldShouldBeginEditing is : section 1 row 1
- textFieldShouldBeginEditing ending
- textFieldDidBeginEditing started
    indexPath for textFieldDidBeginEditing is : section 1 row 1 text 3 tag 1
- textFieldDidBeginEditing ending
- textFieldDidEndEditing ending  <-- end of delete processing
- textFieldDidBeginEditing started
- textFieldDidBeginEditing ... setting responderIndexPath section 0 row 0
    indexPath for textFieldDidBeginEditing is : section 0 row 0 text 123 tag 0
- textFieldDidBeginEditing ending

从日志的最后一部分可以看出,textFieldDidEndEditing完成后,调用textFieldDidBeginEditing,但返回第 0 节和第 0 行(行始终保持显示和可见(

我既不明白为什么调用它,也不明白为什么它没有返回正确的 indexPath。 可以看出,返回值的文本(在本例中为输入的值 123(,我已经用其他数据和其他行(对于文本字段的文本和标签(验证了这一点

也许我在textFieldDidEndEditing中设置becomeFirstReponsder不正确,尽管如果这是真的,我不知道在哪里处理这个问题

希望有人能更好地理解这一点可以帮助我,正如你所知道的,我已经经历了几个小时而没有任何解决方案。

谢谢伊兹

编辑 1:代码中,调用的所有内容在textFieldDidEndEditing完成之前都会becomeFirstReponder。 当您在 cellForRowAtIndexPath 完成后查看日志时,它会两次进入并存在文本字段,一次是针对刚刚删除的行下方的行,另一次是当它为 indexPath 的部分/行返回 0 时再次? 我不明白是什么事件序列导致这篇文章表重新加载方法触发

编辑2:是只有我一个人,还是日志末尾的textFieldDidBeginEditing之前没有textfieldSHOULDbeginEditing似乎很奇怪? 是我在textFieldDidEndEditing期间通过重新加载桌子来搞砸内部流程吗? 有没有更好的地方来执行此操作(即删除一行并重新加载表视图以显示 UI 更新(?

好的,回答我自己的问题...

经过大量调试,似乎...

我通过在textfieldDidEndEditing期间重新加载表视图来推断标准 UI 工作流

调用 reloaddata 会破坏表视图单元格(以及单元格中包含的所有对象,即。UITextFields,UILabels等(并重新创建它们,导致

通过将此调用移动到独立按钮,它按预期运行,而无需 indexPath 返回 0(当它到达这一点时,我仍然不完全理解调用堆栈,但是嘿(

现在我的大问题是决定何时以编程方式调用重新加载,因为我希望它被赶出离开文本字段的事件......我感到另一个问题即将到来:\

希望我的漫谈能帮助将来尝试做类似事情的人......

最新更新