iOS开发 在cell中找到它在tableView中的indexPath

来源:互联网 发布:淘宝库房分拣员累吗 编辑:程序博客网 时间:2024/05/21 03:25

1.这个方法是在cell中用的(比如我在cell中写了一个button的点击方法,我想在点击方法中找到cell的indexPath并通过通知传出去,在其他任何地方用)

UITableView *tableView;    float Version=[[[UIDevice currentDevice] systemVersion] floatValue];//(设备判断)    if(Version>=7.0){        tableView = (UITableView *)self.superview.superview;    }else{        tableView=(UITableView *)self.superview;    }    NSIndexPath *indexPath= [tableView indexPathForCell:self];    NSNotification * notification = [NSNotification notificationWithName:@"clickNumber" object:self userInfo:@{@"value":[NSString stringWithFormat:@"%ld",(long)[indexPath row]]}];    [[NSNotificationCenter defaultCenter] postNotification:notification];

2.接值

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickNumber:) name:@"clickNumber" object:nil];- (void)clickNumber:(NSNotification *)notification{    NSString *string = [notification valueForKey:@"userInfo"][@"value"];    NSLog(@"%@",string);}
0 0
原创粉丝点击