八点钟学院:由“虎扑体育app”的这个效果,来谈谈UITableView编辑模式

作为一名jr,虎扑体育几乎是我每天都得打开的app,赛季期间自不必说,长草期也喜欢看看八卦和交易动态。

 

那么,我们来看看这款app里的这个效果图:

 

 查看更多精彩图片


 八点钟学院:谈谈UITableView编辑模式

 

这是一个非常标准的ios UITableView的编辑模式,有添加、删除、移动三种编辑效果。

 

当你新建UITableView时,是不会有编辑模式的,要实现它的编辑模式,需要按下面的步骤:

 

1、开启tableView的编辑效果

设置UITableView的editing属性为YES,或调用setEditing: animated方法,后者有动画

 

2、实现 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 方法

这个方法是确定是否哪些行是有编辑效果。如果在1的步骤里设置了editing属性为YES,那么即使2这个方法不实现,默认是全部都有编辑效果的。

 

3、实现 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 方法

这个方法是决定每一行的编辑类型是什么,即:UITableViewCellEditingStyle,是一个枚举

 

 

 

分别是代表没有编辑类型、删除类型(cell左边是绿色圆,中间红色线)、添加类型(cell左边是蓝色圆,中间是加号)。值得注意的是移动的编辑类型是不在这个方法里表现的,要想有移动效果,得看下面的方法

 

4、实现  - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 方法

这个方法是确定哪些行是有移动模式的。如果不实现这个方法,如果你代码里有moveRowAtIndexPath这个方法,那么每一行也是有移动模式的(这个方法里面可以不写任何代码,仅仅只要有这个方法在,就会让每一行都有移动模式)

 

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

 

5、实现 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 方法

前面4个步骤已经让我们每一行按照自己的约定有了编辑模式,那么要实现编辑效果:删除数据,增加数据就得看5这个方法了,这里我贴一下我的代码,实现虎扑体育的删除和插入效果:

#pragma mark - tableView编辑模式:删除和插入

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

{

    NSLog(@"commitEditingStyle");

    

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        //容易忽视的问题:删除和插入 分别操作完,textArr和allTextArr数据操作别合在一起

        //否则程序会崩溃

        

        //删除操作

        NSUInteger index = indexPath.row;

        NSString *text = [textArr objectAtIndex:index];

        [textArr removeObjectAtIndex:index];

        

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

        

        //插入操作

        [allTextArr addObject:text];

        NSIndexPath *insertToIndexPath = [NSIndexPath indexPathForRow:allTextArr.count-1 inSection:1];

        [tableView insertRowsAtIndexPaths:@[insertToIndexPath] withRowAnimation:UITableViewRowAnimationBottom];

        

    } else if (editingStyle == UITableViewCellEditingStyleInsert) {

        

        //插入操作

        NSUInteger index = indexPath.row;

        NSString *text = [allTextArr objectAtIndex:index];

        [textArr addObject:text];

        NSIndexPath *insertToIndexPath = [NSIndexPath indexPathForRow:textArr.count-1 inSection:0];

        [tableView insertRowsAtIndexPaths:@[insertToIndexPath] withRowAnimation:UITableViewRowAnimationFade];

        

        //删除操作

        [allTextArr removeObjectAtIndex:index];

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

        

    }

}

到这里,其实整个编辑模式基本讲完了,但是!大家会发现你自己实现的代码,上面section0的那些行不仅仅能在section0里移动,还能够移动到下面section1里,这不符合我们想要的效果!这种时候该怎么办呢?!

 

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath

秘密就在上面这个方法,大家可以看看效果

八点钟学院:谈谈UITableView编辑模式

查看更多精彩图片

了解更多IOS高级开发知识请关注腾讯课堂八点钟学院

https://ke.qq.com/course/171725

官方网址:http://www.8pmedu.com/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值