IOS 通过UITableView上的Button传递参数

来源:互联网 发布:淘宝客活动报名技巧 编辑:程序博客网 时间:2024/05/24 03:20

在做IOS项目时,经常会碰到需要自定义cell的情况,并且在cell上面有Button,点击Button的时候需要响应事件,这个我们可以需要传递一些参数,但是仅仅通过Button没有办法传递我们的参数,这个时候我们就需要找到承载Button的cell,通过cell传递一些参数。

下面介绍点击自定义的cell上面的Button传递一些参数,首先我们自定义一个cell,在cell中定义一下我们要传递的参数,在这里我们定义一个id,点击button事件的时候我们获取这个id,然后根据这个id去执行相应的操作,在Button点击的事件里面获取cell对象的方法有如下两种方法。


 [cell.btnview addTarget:selfaction:@selector(viewdetailinfo:)forControlEvents:UIControlEventTouchUpInside];


方法一

-(void)viewdetailinfo:(UIButton *)sender{    CGRect btnRect=[[sender superview] superview].frame;    //假设我们自定义的cell时FriendCell    for(FriendCell *cell in [self.tableview visibleCells])//遍历cell    {        CGRect cellframe=cell.frame;        if(CGRectIntersectsRect(btnRect, cellframe))//通过位置判断cell        {            //点击的button所在的cell,可以通过cell获取参数        }    }}

方法二

-(void)viewdetailinfo2:(UIButton *)sender{    UIView * v=[sender superview];    FriendCell *cell=(FriendCell *)[v superview];//找到cell    NSIndexPath *indexPath=[self.tableview indexPathForCell:cell];//找到cell所在的行}

以上两种方法可以传递cell上面自定义Button点击事件的参数。

原创粉丝点击