TableViewCell子视图不能在IOS 7中访问

我在我的应用程序中使用自定义的TableViewCell 。 它工作正常与IOS 6,但我在ios 7中出现错误。当我访问UITableViewCell子视图发生错误

看到下面的代码,我得到错误

 - (void)addButtonClicked:(UIButton *)button { Product *product = [productsArray objectAtIndex:button.tag]; NSString *code = product.code; OTATableCell *cell = (OTATableCell *) [[button superview] superview]; cell.priceLabel; // here i get error } 

显示的错误是:

 -[UITableViewCellScrollView priceLabel]: unrecognized selector sent to instance 0x15d0ff80 

请帮我解决这个问题。 提前致谢。

需要更多的superView调用:

  OTATableCell *cell = (OTATableCell *)[[[button superview] superview] superview]; 

你可以检查如下内容:

对于iOS> = 7:

  NSLog(@"%@",[[sender superview] class]); //UITableViewCellContentView NSLog(@"%@",[[[sender superview] superview] class]); //UITableViewCellScrollView NSLog(@"%@",[[[[sender superview]superview]superview] class]); //UITableViewCell 

对于iOS <7:

  NSLog(@"%@",[[sender superview] class]); //UITableViewCellContentView NSLog(@"%@",[[[sender superview] superview] class]); //UITableViewCell NSLog(@"%@",[[[[sender superview]superview]superview] class]); //UITableView 

编辑:如果你不想依靠superView属性:

 UIButton *button = (UIButton *)sender; CGRect buttonFrame = [button convertRect:button.bounds toView:self.table]; NSIndexPath *indexPath = [self.table indexPathForRowAtPoint:buttonFrame.origin]; 

//使用indexPath访问单元格:

 UITableViewCell *cell=[self.table cellForRowAtIndexPath:indexPath]; cell.label=@"setText"; 

问题是表格视图单元格的子视图。 在ios 7中有一个额外的内容视图,所以你需要检查一个超级视图

 float fVersion=[[[UIDevice currentDevice] systemVersion] floatValue]; if(fVersion>=7.0) { OTATableCell *cell = (OTATableCell *)[[[button superview] superview] superview]; } else { OTATableCell *cell = (OTATableCell *)[[button superview] superview]; } 

要么

在cellForRowAtIndexPath中,

设置您添加到单元格上的button的辅助function标识符如下

[ button setAccessibilityIdentifier:[NSString stringWithFormat:@"%d",indexPath.row]];

然后在下面的代码中访问单元格的位置

 NSIndexPath *iIndexRowId =[NSIndexPath indexPathForRow:[[button accessibilityIdentifier] intValue] inSection:0]; OTATableCell *objCell1=(OTATableCell*)[_YouTableName cellForRowAtIndexPath:iIndexRowId]; 

虽然你有正确的答案,但我强烈反对做杂技与superviews。 而更多的标准OOPS方式,很容易做到所需的定制。

你可以创build一个uitableviewcell子类

在它的所有初始化

 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 

方法并保存并使用reuseIdentifier来识别和更改您的单元格外观。 这样你可以灵活地在你的单元格中添加n个方法,并且仍然保持你的单元格为行方法清理。

您不能依赖以UI开始的类中的视图层次结构。 这些类中的视图层次结构可以并且将会改变,恕不另行通知 不要做超级舞蹈。 也许苹果会再次改变iOS8的视图层次结构。

有一个更好的方法来做到这一点。 您可以在tableview中查询tableView中特定点的indexPath。 您可以通过转换button框架来获得这一点。 这将始终有效,因为它不依赖于实现细节。

你这样做:

 - (void)addButtonClicked:(UIButton *)button { CGPoint buttonOriginInTableView = [button convertPoint:CGPointZero toView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonOriginInTableView]; Product *product = [productsArray objectAtIndex:indexPath.row]; NSString *code = product.code; OTATableCell *cell = (OTATableCell *)[self.tableView cellForRowAtIndexPath:indexPath]; cell.priceLabel; } 

显然你的问题是,苹果从来没有答应过你在不同版本的iOS中的单元结构。 所以,依靠superview的superview是一个坏主意。 如果您需要访问与button相同的单元格中的标签,请将其引用保存为button属性。 我的意思是

 @interface MyButton:UIButton @property (nonatomic, weak) UILabel* priceLabel; @end 

并在configuration单元格时分配此属性。

请记住:从来没有硬cade版本! 像<7.0和> = 7.0,因为你不知道8.0版本的样子。

依靠任何内置控件的子视图的顺序是不是一个好主意,因为它的苹果的领土,他们可以随时更改它没有任何通知,因为他们在iOS 7中。

您可以inheritanceUIButton类并添加NSIndexPath类的属性。 在cellForRowAtIndexPath:方法中创build单元格时,将input参数indexpath的值分配给此属性。

点击button时,使用方法[tableView cellForRowAtIndexPath:indexPath]来获取单元格。 这个方法返回类UITableViewCell的对象,在你的情况下,返回自定义单元类。

希望这可以帮助!

您可以访问基于访问的tableview单元格,而不pipe您使用的是哪个版本。此代码将独立于正在运行的操作系统。

  UITableViewCell *cell; UIView * targetView; id superView = [targetView superview]; while (![superView isKindOfClass:[UITableViewCell class]]) { superView = [superView superview]; } cell = (UITableViewCell *)superView; 

现在cell保存UITableViewCell