indexPathForCell从ios7开始返回nil [英] indexPathForCell returns nil since ios7
问题描述
我的应用程序在ios6.1下正常运行。尝试了ios7模拟器,以下部分不起作用:
my app was running fine under ios6.1. tried the ios7 simulator and the following part does not work:
EditingCell *cell = (EditingCell*) [[textField superview] superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSLog(@"the section is %d and row is %d", indexPath.section, indexPath.row);
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *rowKey = [[keysForRows objectAtIndex: section] objectAtIndex: row];
总是来:
the section is 0 and row is 0
尽管另一部分/行是选择。
有人知道为什么这在ios7下不起作用吗?
although another section / row were selected. Has someone an idea why this does not work under ios7?
推荐答案
你找到封闭表的方法文本字段的视图单元格是脆弱的,
因为它假设一个固定的视图层次结构(似乎在
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;
一个完全不同但经常使用的方法是使用行$ b标记文本字段$ b number:
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.
这篇关于indexPathForCell从ios7开始返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!