UITableView 编辑状态下的批量选择与左滑删除等随笔

/**

 创建编辑按钮

 */

- (void)createEdoitBtn{

    UIButton *btn = [[UIButtonalloc]initWithFrame:CGRectMake(20,60,80,30)];

    btn.backgroundColor = [UIColorredColor];

    

    [btn setTitle:@"编辑"forState:UIControlStateNormal];

    

    [btn addTarget:selfaction:@selector(edoitbtn:)forControlEvents:UIControlEventTouchUpInside];

    

    [self.viewaddSubview:btn];


}



/**

 编辑事件


 @param btn 编辑按钮

 */

- (void)edoitbtn:(UIButton *)btn{

    btn.selected = !btn.selected;

    

    if (btn.selected ) {

        //移除之前选中的内容

        if (self.selectorPatnArray.count > 0) {

            [self.selectorPatnArrayremoveAllObjects];

        }

        [btn setTitle:@"完成"forState:UIControlStateNormal];

        //进入编辑状态

        [self.wTableViewsetEditing:YESanimated:YES];

        [self.wTableViewsetAllowsSelectionDuringEditing:YES];

    }else{

        [btn setTitle:@"编辑"forState:UIControlStateNormal];

        [self.wTableViewsetEditing:NOanimated:YES];

    }

}



//行的点击事件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    

   

    [selfchangRowBackGroud:indexPath];

    

    //选中数据

    [self.selectorPatnArrayaddObject:self.dataSource[indexPath.row]];

    

     NSLog(@"加入--self.selectorPatnArray:%@",self.selectorPatnArray);

}


//点击已选中的行调这个方法

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

    //从选中中取消

    if (self.selectorPatnArray.count > 0) {

        

        [self.selectorPatnArrayremoveObject:self.dataSource[indexPath.row]];

    }

    NSLog(@"删除---self.selectorPatnArray:%@",self.selectorPatnArray);


}



-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

    returnYES;

}


//移动

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

    //  需要的移动行

    NSInteger Row1 = [sourceIndexPathrow];

    // 获取移动某处的位置

    NSInteger Row2 = [destinationIndexPathrow];

    // 从数组中读取需要移动行的数据

    id object = [self.dataSourceobjectAtIndex:fromRow];

    // 在数组中移动需要移动的行的数据

    [self.dataSourceremoveObjectAtIndex:Row1];

    // 把需要移动的单元格数据在数组中,移动到想要移动的数据前面

    [self.dataSourceinsertObject:objectatIndex:Row2];

}


//显示的状态。删除。增减。没有

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (self.wTableView.editing) {// 当进入编辑页面时就显示批量选择

        returnUITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;

    }else{

        returnUITableViewCellEditingStyleDelete;//其他时候就左滑显示各种按钮

    }

}

/**

 *  左滑cell时展示多个不同的按钮

 */

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewRowAction *action0 = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormaltitle:@"置顶"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {

        NSLog(@"点击了置顶");

        // 收回左滑出现的按钮(退出编辑模式)

        tableView.editing =NO;

        

    }];

    UITableViewRowAction *action1 = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaulttitle:@"删除"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {

        

        [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];

        

        // 收回左滑出现的按钮(退出编辑模式)

        tableView.editing =NO;

    }];

    

    

    

    return@[action1, action0];

}


更多详细代码:http://download.csdn.net/detail/qq_33646395/9754698

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值