[英]Getting UITableViewCell with superview in iOS 7
I'm getting the UITableViewCell a UIButton belongs to like this: 我得到的UITutView属于UIButton属于这样:
-(void)buttonHandler:(UIButton *)button {
OrderCell *cell = [[button superview] superview];
NSLog(@"cell.item = %@", cell.item.text);
And it works fine in anything before iOS 7. But gives me: 在iOS 7之前它可以正常工作。但是给了我:
[UITableViewCellScrollView item]: unrecognized selector sent to instance 0x17ae2cf0 [UITableViewCellScrollView item]:无法识别的选择器发送到实例0x17ae2cf0
if I run the app in iOS 7. BUT if I do: 如果我在iOS 7中运行该应用程序。但如果我这样做:
-(void)buttonHandler:(UIButton *)button {
OrderCell *cell = [[[button superview] superview] superview];
NSLog(@"cell.item = %@", cell.item.text);
Then it works in iOS 7 but not earlier?!?!?! 然后它可以在iOS 7中运行,但不是更早???!?!
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);
but WTF is going on!? 但是WTF还在继续!? Does anyone know why this happens?
有谁知道为什么会这样?
Thanks! 谢谢!
A better solution is to add a category for UIView(SuperView), and calling it by: 更好的解决方案是为UIView(SuperView)添加一个类别,并通过以下方式调用它:
UITableViewCell *cell = [button findSuperViewWithClass:[UITableViewCell class]]
This way, your code works for all future and past iOS versions 这样,您的代码适用于所有未来和过去的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
The best way to do this is: 最好的方法是:
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
UITableViewCell *cell = (UITableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];
To complete the answer of @thomas-keuleers this is the swift method: 要完成@ thomas-keuleers的答案,这是一个快速的方法:
extension UIView {
func findSuperViewWithClass<T>(superViewClass : T.Type) -> UIView? {
var xsuperView : UIView! = self.superview!
var foundSuperView : UIView!
while (xsuperView != nil && foundSuperView == nil) {
if xsuperView.self is T {
foundSuperView = xsuperView
} else {
xsuperView = xsuperView.superview
}
}
return foundSuperView
}
}
and you simply call like that: 而你只是这样打电话:
child.findSuperViewWithClass(TableViewCell)
A shorter Version in swift 5 swift 5中的较短版本
extension UIView {
var xsuperView = self.superview
while xsuperView != nil {
if let superView = xsuperView.self as? T {
return superView
} else {
xsuperView = xsuperView?.superview
}
}
}
if ([[button superView] isKindOfClass:[UITableViewCell class]]) {
}
else //check next :
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.