indexPathForCell在IOS 7下返回NULL,在IOS 6下工作正常 [英] indexPathForCell returning NULL under IOS 7, works fine under IOS 6

查看:91
本文介绍了indexPathForCell在IOS 7下返回NULL,在IOS 6下工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望Apple现在可以接受IOS 7的应用程序,我们可以谈谈它吗?

I'm hoping now apps for IOS 7 are being accepted by Apple we can talk about it?

我正在尝试修复我的应用程序以便它能够正常运行IOS 7并在今晚取得了一些非常好的进展。我有两个悬而未决的问题,但会分别发布。第一个问题是这一行(我在调试之后添加了一个日志行)...

I'm trying to fix my app so it works on IOS 7 and have made some very good progress this evening. I have two outstanding issues, but will post them separately. The first issue is this line (I added a log line after to debug it) ...

 NSIndexPath *indexPath = [settingsTableView indexPathForCell:(UITableViewCell *)[sender superview]];
 NSLog(@"Changed %@ <-**-> %@", [sender superview], indexPath );

日志显示....

IOS7

 2013-09-11 22:18:02.985 BusTimes[21013:a0b] Changed <UITableViewCellScrollView: 0xbfc1c80; frame =  (0 0; 320 44); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0xbfc1e70>; layer = <CALayer: 0xbfc1540>; contentOffset: {0, 0}> <-**-> (null)

 2013-09-11 22:18:08.577 BusTimes[21013:a0b] Changed <UITableViewCellScrollView: 0xbfc62d0; frame = (0 0; 320 44); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0xbfc64c0>; layer = <CALayer: 0xbfc5b90>; contentOffset: {0, 0}> <-**-> (null)

IOS 6

2013-09-11 22:25:34.495 BusTimes[2116:907] Changed <UITableViewCell: 0x20862f40; frame = (0 179; 320 44); text = 'Reminder Mins: 5'; autoresize = W; layer = <CALayer: 0x208523d0>> <-**-> <NSIndexPath 0x20861430> 2 indexes [0, 3]
2013-09-11 22:25:47.499 BusTimes[2116:907] Changed <UITableViewCell: 0x20865180; frame = (0 223; 320 44); text = 'AutoRefresh Secs: 60'; autoresize = W; layer = <CALayer: 0x20865340>> <-**-> <NSIndexPath 0x2085c0d0> 2 indexes [0, 4]

所以在IOS 7上,indexPath总是(null)和.section和.row总是0.但这在IOS 6上工作正常并且返回.section 0和第3行,以及.row 4.有人可以建议我如何让IOS 7以同样的方式为我行事吗?

So on IOS 7 indexPath is always (null) and .section and .row are always 0. But this works fine on IOS 6 and returns .section 0 and row 3, and .row 4. Can anyone suggest how I might get IOS 7 to behave in the same way for me?

非常感谢提前。

血浆

*解决方案 - 我在评论后将代码更改为此

UIView *view = sender;

while (![view isKindOfClass:[UITableViewCell class]]) {
    view = [view superview];
}

NSIndexPath *indexPath = [settingsTableView indexPathForCell:(UITableViewCell *)view];


推荐答案

对视图层次结构不做任何假设通常更安全一个细胞。在过去,我在UIView上使用了类似方法的类别来查找超级视图,这是一个单元格:

It's usually safer to make no assumptions about the view hierarchy of a cell. In the past, I've used a category with methods like this on UIView to find the superview which is a cell:

- (UIView*)ancestorOrSelfWithClass:(Class)cls {
  if ([self isKindOfClass:cls]) {
    return self;

  } else if (self.superview) {
    return [self.superview ancestorOrSelfWithClass:cls];

  } else {
    return nil;
  }
}

所以你要求 [sender ancestorOrSelfWithClass:[UITableViewCell class]] 获取实际的单元格,无论单元格中存在什么层次结构,它都可以工作。

So you'll ask for [sender ancestorOrSelfWithClass:[UITableViewCell class]] to get the actual cell, and it'll work no matter what hierarchy exists within the cell.

这篇关于indexPathForCell在IOS 7下返回NULL,在IOS 6下工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆