0

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

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

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

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

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

textFieldDidBeginEditing我保存正在编辑的 textField 的 indexPath 期间,使用:

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),我已经用其他数据和其他行验证了这一点(对于文本字段的文本和标签)

也许我的 inside 设置becomeFirstReponsdertextFieldDidEndEditing正确,但如果这是真的,我不知道在哪里处理这个

希望有人对此有更好的理解可以帮助我,正如你所知道的,我已经经历了几个小时没有任何解决方案

谢谢伊兹

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

编辑 2:只是我,还是 在日志末尾之前没有 NO 似乎真的很奇怪?是不是我通过在 期间重新加载表格来搞砸内部流程?有没有更好的地方来做到这一点(即删除一行并重新加载表格视图以显示 UI 更新)?textfieldSHOULDbeginEditingtextFieldDidBeginEditingtextFieldDidEndEditing

4

1 回答 1

6

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

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

我通过在期间重新加载表格视图来干扰标准 UI 工作流程textfieldDidEndEditing

调用 reloaddata 会破坏 tableview 单元格(以及单元格中包含的所有对象,即 UITextFields、UILabels 等)并重新创建它们,从而导致

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

现在我的大问题是决定何时以编程方式调用重新加载,因为我希望它摆脱离开文本字段的事件......我觉得另一个问题来了:\

希望我的胡言乱语可以帮助将来尝试做类似事情的人...

于 2012-08-02T12:50:52.480 回答