在iOS 7中使用superview获取UITableViewCell [英] Getting UITableViewCell with superview in iOS 7
问题描述
我得到的UITutView属于这样的UITutViewCell:
I'm getting the UITableViewCell a UIButton belongs to like this:
-(void)buttonHandler:(UIButton *)button {
OrderCell *cell = [[button superview] superview];
NSLog(@"cell.item = %@", cell.item.text);
在iOS 7之前的任何东西都能正常工作。但是给了我:
And it works fine in anything before iOS 7. But gives me:
[UITableViewCellScrollView item]:无法识别的选择器发送到实例0x17ae2cf0
[UITableViewCellScrollView item]: unrecognized selector sent to instance 0x17ae2cf0
如果我在iOS 7中运行应用程序。但是如果我这样做:
if I run the app in iOS 7. BUT if I do:
-(void)buttonHandler:(UIButton *)button {
OrderCell *cell = [[[button superview] superview] superview];
NSLog(@"cell.item = %@", cell.item.text);
然后它可以在iOS 7中运行但不是更早???!?!
Then it works in iOS 7 but not earlier?!?!?!
我通过这样做来解决这个问题:
I'm circumventing the issue by doing this:
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还在继续!?
有谁知道为什么会这样?
but WTF is going on!? Does anyone know why this happens?
谢谢!
推荐答案
更好的解决方案是为UIView(SuperView)添加一个类别,并通过以下方式调用它:
A better solution is to add a category for UIView(SuperView), and calling it by:
UITableViewCell *cell = [button findSuperViewWithClass:[UITableViewCell class]]
这样,你的代码适用于所有未来和过去的iOS版本
This way, your code works for all future and past iOS versions
@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中使用superview获取UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!