UITableView的编辑模式

24 篇文章 0 订阅

#pragma mark -- 设置Cell移动模式


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

    returnYES;

}


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

    

}



#pragma mark -- 编辑方法


-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    //能够编辑方法

    returnYES;

}


//设置编辑模式

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

    returnUITableViewCellEditingStyleInsert;

    //return UITableViewCellEditingStyleDelete;


}



//编辑事件回调方法,此方法实现后,左划删除动画才会出现

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

    if(editingStyle==UITableViewCellEditingStyleDelete){

        NSLog(@"删除");

        

    }elseif(editingStyle==UITableViewCellEditingStyleInsert){

        NSLog(@"添加");

        

        UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"输入添加内容"message:nilpreferredStyle:UIAlertControllerStyleAlert];

        

        [alert addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {

            [textField setPlaceholder:@"请输入姓名"];

            

        }];

        [alert addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {

            [textField setPlaceholder:@"请输入电话号码"];

        }];


        UIAlertAction *confirm = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {

            //这里来写添加和显示数据的代码

            //拿到

            NSString *name = alert.textFields[0].text;

            NSString *phone = alert.textFields[1].text;

            //转换成对象

            AddressListModel *model = [[AddressListModelalloc]initWithDictionary:@{@"name":name,@"phone":phone}];

            [self.dataSourceinsertObject:model atIndex:indexPath.row+1];

            

            //[tableView reloadData];

            

            //创建一个indexPath

            NSIndexPath *newIndexPath = [NSIndexPathindexPathForRow:indexPath.row+1inSection:indexPath.section];

            [self.tableViewinsertRowsAtIndexPaths:@[newIndexPath]withRowAnimation:UITableViewRowAnimationAutomatic];

        }];

        

        UIAlertAction *cancel = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

        

        

        [alert addAction:confirm];

        [alert addAction:cancel];

        

        [selfpresentViewController:alertanimated:YEScompletion:nil];

        

 

    }else{

        NSLog(@"None");

    }

    

}


//设置多个编辑标签

-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{

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

        NSLog(@"点击了%ld行的删除",indexPath.row);

        [self.dataSourceremoveObjectAtIndex:indexPath.row];

        //[self.tableView reloadData];

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

        

    }];

    

    UITableViewRowAction *markA = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormaltitle:@"标记未读"handler:^(UITableViewRowAction *_Nonnull action,NSIndexPath * _Nonnull indexPath) {

        NSLog(@"点击了%ld行的标记",indexPath.row);

    }];

    

    return @[deleteAction,markA];

}


#pragma mark -- 数据源方法





-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [self.dataSourcecount];

}




-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *cellIdentifier =@"ContactCell";

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellIdentifier];

    if(!cell){

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:cellIdentifier];

    }

    

    //获得对应的数据

    AddressListModel *contact =self.dataSource[indexPath.row];

    cell.textLabel.text = contact.name;

    cell.detailTextLabel.text = contact.phone;

    

    return cell;

    

}

















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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值