我的应用程序在ios6.1下运行得很好。已尝试ios7模拟器,但以下部分不起作用:
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下不起作用吗?
发布于 2013-09-11 22:20:26
查找文本字段的“封闭”表视图单元格的方法是脆弱的,因为它假设了固定的视图层次结构(这似乎在iOS 6和iOS 7之间发生了变化)。
一种可能的解决方案是在视图层次结构中向上遍历,直到找到表视图单元:
UIView *view = textField;
while (view != nil && ![view isKindOfClass:[UITableViewCell class]]) {
view = [view superview];
}
EditingCell *cell = (EditingCell *)view;
一种完全不同但经常使用的方法是用行号“标记”文本字段:
cell.textField.tag = indexPath.row; // in cellForRowAtIndexPath
然后只需在文本字段委托方法中使用该标记。
发布于 2013-09-19 22:49:41
我发现细胞的方式和你一样。现在,如果我在单元格中有一个按钮,并且知道我所在的表视图,我会使用这个快速方法。它将返回tableviewcell。
-(UITableViewCell*)GetCellFromTableView:(UITableView*)tableView Sender:(id)sender {
CGPoint pos = [sender convertPoint:CGPointZero toView:tableView];
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:pos];
return [tableView cellForRowAtIndexPath:indexPath];
}
发布于 2017-11-06 22:36:58
在iOS 11中遇到了这个问题,而不是在9或10中,我使用@drexel-sharp前面详细描述的技术重写了func indexPath(for cell: UITableViewCell) -> IndexPath?
方法:
override func indexPath(for cell: UITableViewCell) -> IndexPath? {
var indexPath = super.indexPath(for: cell)
if indexPath == nil { // TODO: iOS 11 Bug?
let point = cell.convert(CGPoint.zero, to: self)
indexPath = indexPathForRow(at: point)
}
return indexPath
}
https://stackoverflow.com/questions/18743099
复制相似问题