首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >自ios7以来,indexPathForCell返回nil

自ios7以来,indexPathForCell返回nil
EN

Stack Overflow用户
提问于 2013-09-11 21:50:41
回答 3查看 11.3K关注 0票数 19

我的应用程序在ios6.1下运行得很好。已尝试ios7模拟器,但以下部分不起作用:

代码语言:javascript
复制
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];

它总是会出现:

代码语言:javascript
复制
the section is 0 and row is 0

尽管选择了另一个区段/行。有人知道为什么这在ios7下不起作用吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-09-11 22:20:26

查找文本字段的“封闭”表视图单元格的方法是脆弱的,因为它假设了固定的视图层次结构(这似乎在iOS 6和iOS 7之间发生了变化)。

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

代码语言:javascript
复制
UIView *view = textField;
while (view != nil && ![view isKindOfClass:[UITableViewCell class]]) {
    view = [view superview];
}
EditingCell *cell = (EditingCell *)view;

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

代码语言:javascript
复制
cell.textField.tag = indexPath.row;   // in cellForRowAtIndexPath

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

票数 30
EN

Stack Overflow用户

发布于 2013-09-19 22:49:41

我发现细胞的方式和你一样。现在,如果我在单元格中有一个按钮,并且知道我所在的表视图,我会使用这个快速方法。它将返回tableviewcell。

代码语言:javascript
复制
-(UITableViewCell*)GetCellFromTableView:(UITableView*)tableView Sender:(id)sender {
    CGPoint pos = [sender convertPoint:CGPointZero toView:tableView];
    NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:pos];
    return [tableView cellForRowAtIndexPath:indexPath];
}
票数 22
EN

Stack Overflow用户

发布于 2017-11-06 22:36:58

在iOS 11中遇到了这个问题,而不是在9或10中,我使用@drexel-sharp前面详细描述的技术重写了func indexPath(for cell: UITableViewCell) -> IndexPath?方法:

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18743099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档