UI day 10 UItabelView 编辑和移动 UItabelViewController


                                     UItabelView 编辑

1.tabelview编辑的步骤:
(1)tabelview处于编辑状态(默认所有的cell都处于编辑状态,默认的编辑样式是删除)
(2)设置哪些cell可以编辑
(3)设置编辑的样式(删除,插入)
(4)提交编辑结果(先修改数据源,在修改UI)
2.使用场景
删除一个下载好的视频,删除联系人
插入一条新的聊天记录等

第一步  让tabelView处于编辑状态
//设置编辑按钮第一步########
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
//重写点击Edit按钮的方法
- (
void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [
super setEditing:editing animated:animated];
   
NSLog(@"%d",editing);//editing1时,可以编辑,editing0时不可编辑
    [(
UITableView *)self.view  setEditing:editing animated:YES];
}
第二步  指定tabelView哪些行可以编辑
#pragma mark        数据源代理方法     TabelView DataSource方法
//2.配置哪些sell可以编辑 编辑第二步#########
-(
BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.section<3) {
//        return YES;
//    }
//    return NO;
   
return indexPath.section < 3? YES : NO;
}

第三步  指定tabelView编辑的样式(添加  删除)
#pragma mark     业务代理方法  TabelView   Delegate方法
//设置tabelView的编辑样式 编辑第三步####
-(
UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.section < 2) {
//        return UITableViewCellEditingStyleDelete;
//    }
//    else
//    {
//        return UITableViewCellEditingStyleInsert;
//    }
   
return indexPath.section < 2 ? UITableViewCellEditingStyleDelete:UITableViewCellEditingStyleInsert;
}


第四步   编辑完成   (先操作数据源,再修改UI)
#pragma mark       数据源代理方法   TabelView   DataSource 方法
//提交编辑操作      第四步
-(
void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
   
//先修改数据源,在更新UI(界面)
   
//1.根据分区所有获取key,确定要删除的cell在哪个分区(egB分区 D分区)
   
NSString *key = self.orderKeys[indexPath.section];
   
//2.根据key值拿到字典中对应的数组
   
NSMutableArray *group = self.dict[key];
   
   
   
   
   
//删除
   
if (editingStyle ==UITableViewCellEditingStyleDelete) {
       
//处理删除操作
       
if (1==group.count) {//删除整个分区
           
//1.先删除数据源,从字典中移除key
            [
self.dict removeObjectForKey:key];
           
//删除索引栏数组中对应的元素
            [self.orderKeys removeObjectAtIndex:indexPath.section];
            //2.更新UI界面
           
//创建一个nsindexset对象,使用分区下标初始化
           
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];
            [tableView
deleteSections:indexSet withRowAnimation:(UITableViewRowAnimationRight)];
          
           
        }
else{//删除对应的cell即可
         
           
//先删除数据源
            [group
removeObjectAtIndex:indexPath.row];
           
//在更新界面
           
//tabelview删除 可以删除多行
            [tableView
deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
        }
       
       
       
       
       
       
    }
else{
       
//添加
       
//1.准备要插入的数据
       
NSDictionary *dic = @{@"name":@"黄凯",@"gender":@"",@"age":@"25",@"phone":@"13838652253",@"says":@"千人斩"};
       
//2.修改数据源
        [group
insertObject:dic atIndex:indexPath.row];
       
//3.更新UI界面
        [tableView
insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];

    }
}

                              UITabelView  移动

1.tabelView移动的步骤:
(1)tabelView处于编辑状态
(2)设置哪些行(cell)可以移动
(3)提交移动结果

第一步   让tabelView处于编辑状态
             TabelView方法

//重写点击Edit按钮的方法
- (
void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [
super setEditing:editing animated:animated];
   
NSLog(@"%d",editing);//editing1时,可以编辑,editing0时不可编辑
    [(
UITableView *)self.view  setEditing:editing animated:YES];
}

第二步  指定tabelView哪些行可以移动
#pragma mark  数据源代理方法         TabelView  DataSource 方法
//设置哪些行可以移动
-(
BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
   
return YES;
}

第三步    移动完成
#pragma mark  数据源代理方法         TabelView  DataSource 方法
//提交移动后的操作 sourceIndexPathcell的原来的位置  destinationIndexPathcell移动之后位置
-(
void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
   
//移动操作不需要更新UI界面,因为移动的时候UI界面已经发生了变化,此时,只需要修改数据源即可:
   
//首先获取cell上展示的数据所在数组
   
//1.取出key
   
NSString *key = self.orderKeys[sourceIndexPath.section];
   
//2.取出字典中key值对应的数组
   
NSMutableArray *mArr = self.dict[key];
   
//3.将原来的数据取出来一份,保存起来
   
NSDictionary *dic = [mArr[sourceIndexPath.row]retain];//引用计数器是2
   
//4.删除数组中原来位置的元素
    [mArr
removeObjectAtIndex:sourceIndexPath.row];//1
   
//5.将元素插入到数组中新的位置
    [mArr
insertObject:dic atIndex:destinationIndexPath.row];//2
   
//6.释放dic
    [dic
release];//1
   
}

#######监测移动过程,实现限制跨区移动##########
      TabelView方法

//如果移动后不是在原来的分区,则取消移动的结果  这是代理里面的方法  sourceIndexPath cell原来的位置  proposedDestinationIndexPathcell移动之后的位置
-(
NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
   
//如果在同一个分区则让cell移动,返回移动后的位置
   
if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
       
return proposedDestinationIndexPath;
    }
else{//如果不在同一分区,返回移动之前的位置
       
return sourceIndexPath;
       
    }
}







                               UITabelViewcontroller
1.UITabelViewcontroller父类是UIViewcontroller
继承自UItabelViewcontroller和继承UIViewcontroller区别
(1)前者自带视图UItabelView,而后者是UIView
(2)前者的不需要指定tabelView dataSource 和  delegate 方法,也不需要付出协议;后者上的tabelView都需要这些操作
(3)前者不需要重写setEditing:animated方法,而后者需要重写这个方法
(4)前者dataSource和delegate中方法有些系统自动帮我们生成,而后者这些方法都要自己写

2.何时需要使用继承自UItabelViewController子类:当前的页面的信息绝大部分都使用cell(以列的形式展示)展示的,此时考虑继承自UItabelViewcontroller


























































































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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值