iOS TableViewcell 全选删除

1.tableview创建

**//测试数组
    self.dateArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];
    //设置tableview
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 52, self.view.width, self.view.height)];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.backgroundColor = RGBCOLOR(239, 240, 241);
    //取消cell线
    //self.myTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    //self.myTableView.editing = NO;
    [self.view addSubview:self.myTableView];
**

2.tableview代理函数

#pragma mark 设置行数 rows
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dateArray.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

![图片展示1](http://img.blog.csdn.net/20161111112142105)
#pragma mark 创建tableviewcell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *myCell = @"mycell";
    MyCollectTabView *cell = [tableView dequeueReusableCellWithIdentifier:myCell];
    if (cell == nil) {
        cell = [[MyCollectTabView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCell];
    }
    //cell的点击效果
    //cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark 允许对cell进行编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//cell 编辑设置

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        [self.dateArray removeObjectAtIndex:indexPath.row];
        [self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView endUpdates];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
    }
}

#pragma mark 点击tableviewcell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击第几行 row == %ld",indexPath.row);
}
#pragma mark 设置cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return FIXWIDTHORHEIGHT(90);
}

3.自己创建一个全选按钮,我只给一个调用的函数

-(void)btnAllSelectAction:(UIButton *)sender
{
    sender.backgroundColor = RGBCOLOR(239, 240, 241);
    //通过遍历所有并选择
    for (int row=0; row<10; row++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
        [self.myTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }

}

图片展示2
4.删除就把数据的data数组清空就可以了,如果不是全部要用数组记录一下

**
有一个需要注意的地方,全选时候内容整体向右滑动一小块
就是把tableviewcell中的img或者label 都放在 cell的 contentView上面就好了
**

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值