iOS Developer

利用Xib自定义创建cell,拿到控件所在row

2016-12-20  本文已影响0人  彭小先生

首先说下思想:

假设每个cell都有一个按钮,当我们点击按钮后要对相应的cell进行操作,这样就需要我们拿到IndexPath.row了。然后才能进行操作。

实战:

  1. 我们要拿到每次点击的按钮。(按钮点击事件中我们可以用代理或者块把按钮传到需要的VC上)
    这里用的是Block:
    @property (copy, nonatomic)void(^changeApplyList)(UIButton *);
    然后在按钮响应事件里就可以传值了(添加下面代码):
 if(_changeApplyList)  //判断是否对属性值操作
    {
        self.changeApplyList(sender);
    }

就下来就是接收值了:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    applyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"123"];
//对块操作  弱应用
    __weak applyListViewController *weakSelf = self;
//拿到按钮
    [cell setChangeApplyList:^(UIButton *sender) {
        [weakSelf changeApplyList:sender];
    }];
    return cell;
}

2.最后就是对传过来按钮操作 拿到所在的cell

-(void)changeApplyList:(UIButton *)sender
{
//这里拿到按钮所在的tableview的cell,一个superview只拿到按钮本身的那个cell。。所以要用两个,如果按钮是在contentview上  需要用三个。。
    UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
//根据tableview拿到所在的位置
    NSIndexPath *indexPath = [_friendsView indexPathForCell:cell];
//我这里是对行数进行操作了
    [_nameArray removeObjectAtIndex:indexPath.row];
}
上一篇 下一篇

猜你喜欢

热点阅读