如何得到自定义UITableViewCell中的按钮所在的cell的indexPath.row

来源:互联网 发布:晨曦软件支持什么系统 编辑:程序博客网 时间:2024/06/06 02:56

在iOS7下面已经无效,因为iOS7的层级关系发生变化

UITableViewCell->UITableViewCellScrollView->UITableViewCellContentView->Your custom view

下面有2种方法解决这个问题

-(void) visitButtonClicked:(UIButton *)sender

{

    // 第一种,兼容所有的版本

    UIView *superView = sender.superview;

    UITableViewCell *foundSuperView = nil;

    

    while (nil != superView && nil == foundSuperView) {

        if ([superView isKindOfClass:[UITableViewCell class]]) {

            foundSuperView = (UITableViewCell *)superView;

        } else {

            superView = superView.superview;

        }

    }

    NSLog(@"founda = %d", [_tableView indexPathForCell:foundSuperView].row);

   

    // 2种,不兼容iOS7以下

    UITableViewCell *cell = (UITableViewCell *)[[[sender superviewsuperviewsuperview];

    

    NSLog(@"foundb = %d", [_tableView indexPathForCell:cell].row);

}


0 0
原创粉丝点击