HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在iOS 7中使用超级视图获取UITableViewCell大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我得到UITableViewCell UIButton属于这样:
-(void)buttonHandler:(UIButton *)button {

    OrderCell *cell = [[button superview] superview];
    NSLog(@"cell.item = %@",cell.item.text);

在iOS 7之前的任何事情都可以正常工作.但是给我:

[UITableViewCellScrollView item]:无法识别的选择器发送到实例0x17ae2cf0

如果我在iOS 7中运行应用程序,但如果我这样做:

-(void)buttonHandler:(UIButton *)button {

    OrderCell *cell = [[[button superview] superview] superview];
    NSLog(@"cell.item = %@",cell.item.text);

那么它可以在iOS 7中工作,但不能早期?

我通过这样做来规避这个问题:

OrderCell *cell;
if([[[UIDevice currentDevice] systemVersion] isEqualToString:@"7.0"])
    cell = [[[button superview] superview] superview];
else
    cell = [[button superview] superview];

NSLog(@"cell.item = %@",cell.item.text);

但是WTF正在进行中!
有人知道为什么会发生这种情况吗?

谢谢!

解决方法

更好的解决方案是为UIView(SuperView)添加一个类别,并通过以下方式调用它:
UITableViewCell *cell = [button findSuperViewWithClass:[UITableViewCell class]]

这样,您的代码适用于所有未来和过去的iOS版本

@interface UIView (SuperView)

- (UIView *)findSuperViewWithClass:(Class)superViewClass;

@end


@implementation UIView (SuperView)

- (UIView *)findSuperViewWithClass:(Class)superViewClass {

    UIView *superView = self.superview;
    UIView *foundSuperView = nil;

    while (nil != superView && nil == foundSuperView) {
        if ([superView isKindOfClass:superViewClass]) {
            foundSuperView = superView;
        } else {
            superView = superView.superview;
        }
    }
    return foundSuperView;
}
@end

大佬总结

以上是大佬教程为你收集整理的在iOS 7中使用超级视图获取UITableViewCell全部内容,希望文章能够帮你解决在iOS 7中使用超级视图获取UITableViewCell所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。