参考自:http://stackoverflow.com/questions/15711645/how-to-get-uitableview-from-uitableviewcell
通过UITableViewCell得到其UITableView
id view = [tableViewCellInstance superview];
while (view && [view isKindOfClass:[UITableView class]] == NO) {
view = [view superview];
}
UITableView *tableView = (UITableView *)view;
通过UITableViewCell上的子View来获得其对应的UITableViewCell
参考自:http://stackoverflow.com/questions/18962771/getting-uitableviewcell-with-superview-in-ios-7
UITableViewCell *cell = [button findSuperViewWithClass:[UITableViewCell class]]
@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
或者
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
UITableViewCell *cell = (UITableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];