UITableViewCell子视图无法在IOS 7中访问

时间:2014-04-25 06:05:31

标签: ios objective-c ios7 uitableview

我在我的应用中使用自定义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

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

7 个答案:

答案 0 :(得分:4)

还需要一次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";

答案 1 :(得分:1)

问题是表视图单元子视图。在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];
    }

或者

in cellForRowAtIndexPath,

设置您在单元格上添加的按钮的辅助功能标识符,如下所示

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

然后,您要在代码

下方访问单元格
NSIndexPath *iIndexRowId =[NSIndexPath indexPathForRow:[[button accessibilityIdentifier] intValue] inSection:0];
OTATableCell *objCell1=(OTATableCell*)[_YouTableName cellForRowAtIndexPath:iIndexRowId];

答案 2 :(得分:0)

显然你的问题是Apple从未向你承诺过不同版本的iOS中单元格的结构。因此,依靠superview的超级视图是一个坏主意。如果需要访问与按钮相同的单元格中的标签,请将其引用保留为按钮属性。我的意思是

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

在配置单元格时分配此属性。

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

答案 3 :(得分:0)

依赖于任何内置控件的子视图顺序并不是一个好主意,因为它的Apple领域可以随时更改它,而不会像在iOS 7中那样通知。

您可以继承UIButton类并添加类NSIndexPath的属性。在cellForRowAtIndexPath:方法中创建单元格时,会将输入参数indexpath的值指定给此属性。

点击按钮时,使用[tableView cellForRowAtIndexPath:indexPath]方法获取单元格。此方法返回类UITableViewCell的对象,在您的情况下,返回自定义单元格类。

希望这有帮助!

答案 4 :(得分:0)

虽然你有正确的答案,但我非常反对与超级视图做杂耍。而且,除了标准的OOPS方式之外,很容易进行所需的定制。

你可以创建一个uitableviewcell子类

在其

中进行所有初始化
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

方法并保存并使用reuseIdentifier来识别和更改您的手机外观。通过这种方式,您可以灵活地在单元格中添加n个方法,并保持单元格的行方法清洁。

答案 5 :(得分:0)

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

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

你这样做:

- (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;
}

答案 6 :(得分:0)

您可以根据访问权限访问tableview单元格,无论您使用的是哪个版本。此代码将独立于其运行的操作系统。

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

现在cell拥有UITableViewCell