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;
一种完全不同但经常使用的方法是用行标记"文本字段号码:
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屋!