============================================================
博文原创,转载请声明出处
============================================================
For iOS >= 8:
NSLog(@"%@",[[sender superview] class]); //UITableViewCell
NSLog(@"%@",[[[sender superview] superview] class]); //UITableViewWrapperView
NSLog(@"%@",[[[[sender superview]superview]superview] class]); //UITableView
For iOS >= 7 && <8:
NSLog(@"%@",[[sender superview] class]); //UITableViewCellContentView
NSLog(@"%@",[[[sender superview] superview] class]); //UITableViewCellScrollView
NSLog(@"%@",[[[[sender superview]superview]superview] class]); //UITableViewCell
For iOS <7:
NSLog(@"%@",[[sender superview] class]); //UITableViewCellContentView
NSLog(@"%@",[[[sender superview] superview] class]); //UITableViewCell
NSLog(@"%@",[[[[sender superview]superview]superview] class]); //UITableView
总之给我的感觉就是乱起八糟,各种判断,这里介绍一种简单的方法。
CGRect senderFrame = [sender convertRect:sender.bounds toView:self.table];
NSIndexPath *indexPath = [self.table indexPathForRowAtPoint: senderFrame.origin];
UITableViewCell *cell=[self.table cellForRowAtIndexPath:indexPath];
因为这个经常调用,我写成了宏
#define INDEXPATH_SUBVIEW_TABLEVIEW(subview,tableview)\
({\
CGRect subviewFrame = [subview convertRect:subview.bounds toView:tableview];\
NSIndexPath *indexPath = [tableview indexPathForRowAtPoint:subviewFrame.origin];\
indexPath;\
})\
#define CELL_SUBVIEW_TABLEVIEW(subview,tableview)\
({\
CGRect subviewFrame = [subview convertRect:subview.bounds toView:tableview];\
NSIndexPath *indexPath = [tableview indexPathForRowAtPoint:subviewFrame.origin];\
UITableViewCell *cell=[_tvItems cellForRowAtIndexPath:indexPath];\
cell;\
})\
也可以使用下面方法
UIView *view = sender;
while (view && ![sender isKindOfClass:[UITableViewCell class]]){
view = view.superview;
}
UITableViewCell *cell=(UITableViewCell*)view;
不过个人觉得如果程序有问题,会造成死循环。。