您的位置:首页 > 产品设计 > UI/UE

UI day 10 UItabelView 编辑和移动 UItabelViewController

2015-12-21 20:54 417 查看
                                     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);//editing为1时,可以编辑,editing为0时不可编辑

    [(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在哪个分区(eg:B分区
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);//editing为1时,可以编辑,editing为0时不可编辑

    [(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 方法

//提交移动后的操作
sourceIndexPath是cell的原来的位置 
destinationIndexPath是cell移动之后位置

-(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原来的位置  proposedDestinationIndexPath是cell移动之后的位置

-(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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UI