且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

indexPathForCell 从 ios7 开始返回 nil

更新时间:2023-11-29 08:13:28

您查找文本字段的封闭"表格视图单元格的方法很脆弱,因为它假定一个固定的视图层次结构(这似乎在iOS 6 和 iOS 7).

Your approach to find the "enclosing" table view cell of a text field is fragile, because is assumes a fixed view hierarchy (which seems to have changed between iOS 6 and iOS 7).

一种可能的解决方案是在视图层次结构中向上遍历,直到找到表格视图单元格:

One possible solution would be to traverse up in the view hierarchy until the table view cell is found:

UIView *view = textField;
while (view != nil && ![view isKindOfClass:[UITableViewCell class]]) {
    view = [view superview];
}
EditingCell *cell = (EditingCell *)view;

一种完全不同但经常使用的方法是用行标记"文本字段号码:

A completely different, but often used method is to "tag" the text field with the row number:

cell.textField.tag = indexPath.row;   // in cellForRowAtIndexPath

然后只需在文本字段委托方法中使用该标签.

and then just use that tag in the text field delegate methods.