ios 八点钟学院

由“虎扑体育app”的这个效果,来谈谈UITableView编辑

2016-11-12  本文已影响118人  腾讯课堂八点钟学院

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

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

这是一个非常标准的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

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

上一篇下一篇

猜你喜欢

热点阅读