ios开发:UITableViewCell的操作

UITableViewCell在iosUITableView开发中是非常重要的一部分,所以在这篇文档,我们进行单独的讲解。本篇主要讲解UITableViewCell的标记、移动、删除、插入功能。

1、标记行

这里讲的标记行指的是单击此行,可以实现在此行右边出现一个勾

为了实现此功能,添加如下代码:

#pragma mark -#pragma mark Table Delegate Methods- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {     UITableViewCell *oneCell = [tableView cellForRowAtIndexPath: indexPath];    if (oneCell.accessoryType == UITableViewCellAccessoryNone) {        oneCell.accessoryType = UITableViewCellAccessoryCheckmark;    } else         oneCell.accessoryType = UITableViewCellAccessoryNone;    [tableView deselectRowAtIndexPath:indexPath animated:YES]; }

代码实现的功能是:单击某一行时,如果该行未被标记,则标记该行;若该行已经被标记,则取消标记。

上面的代码实际上就是修改UITableViewCell的accessoryType属性,这个属性可以设为四个常量:

UITableViewCellAccessoryCheckmarkUITableViewCellAccessoryDetailDisclosureButtonUITableViewCellAccessoryDisclosureIndicatorUITableViewCellAccessoryNone

效果如下所示:

            

   UITableViewCellAccessoryCheckmark            UITableViewCellAccessoryDetailDisclosureButton

                 

UITableViewCellAccessoryDisclosureIndicator                   UITableViewCellAccessoryNone

2、移动行

想要实现移动或者删除行这样的操作,需要启动表格的编辑模式。使用的是setEditing:animated:方法。

实现如下代码:

//启动表格的编辑模式[self.myTableView setEditing:YES animated:YES];

 在@end之前添加代码:

//打开编辑模式后,默认情况下每行左边会出现红的删除按钮,这个方法就是关闭这些按钮的- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {     return UITableViewCellEditingStyleNone; } //这个方法用来告诉表格 这一行是否可以移动- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {     return YES; }//这个方法就是执行移动操作的- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)        sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {    NSUInteger fromRow = [sourceIndexPath row];     NSUInteger toRow = [destinationIndexPath row];         id object = [list objectAtIndex:fromRow];     [list removeObjectAtIndex:fromRow];     [list insertObject:object atIndex:toRow]; }

editingStyleForRowAtIndexPath这个方法中用到了常量UITableViewCellEditingStyleNone,它表示不可编辑,这里的编辑指的是删除和插入。表示表格行的编辑模式的常量有:

UITableViewCellEditingStyleDeleteUITableViewCellEditingStyleInsertUITableViewCellEditingStyleNone

顾名思义,第一个表示删除,第二个表示插入,第三个表示不可编辑。

如果将editingStyleForRowAtIndexPath方法中的UITableViewCellEditingStyleNone换成上面三个值,则它们运行的效果依次如下图所示:

      

运行,可以看到实现了行的移动:

需要注意的是在编辑状态下,无法选择行。

3、删除行

删除某行,其实比较简单了。

将editingStyleForRowAtIndexPath方法中的UITableViewCellEditingStyleNone修改成UITableViewCellEditingStyleDelete。

 在@end之前添加代码:

//这个方法根据参数editingStyle是UITableViewCellEditingStyleDelete//还是UITableViewCellEditingStyleDelete执行删除或者插入- (void)tableView:(UITableView *)tableView commitEditingStyle:    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {    NSUInteger row = [indexPath row];    if (editingStyle == UITableViewCellEditingStyleDelete) {        [self.list removeObjectAtIndex:row];         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]                         withRowAnimation:UITableViewRowAnimationAutomatic];     }}

 

 

UITableViewRowAnimationAutomatic表示删除时的效果,类似的常量还有:

UITableViewRowAnimationAutomaticUITableViewRowAnimationTopUITableViewRowAnimationBottomUITableViewRowAnimationLeftUITableViewRowAnimationRightUITableViewRowAnimationMiddleUITableViewRowAnimationFadeUITableViewRowAnimationNone

运行,看看效果:

      

刚运行时显示如左边的图片,点击某一行左边的圆圈图标,会显示如中间图片所示。然后点击Delegate按钮,那一行就会被删除掉,如右边的那张图片所示,它显示的是删除时的效果。

4、插入行

这个与删除行类似。

首先将editingStyleForRowAtIndexPath方法中的UITableViewCellEditingStyleDelete修改成UITableViewCellEditingStyleInsert。

else {    //我们实现的是在所选行的位置插入一行,因此直接使用了参数indexPath    NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];    //同样,将数据加到list中,用的row    [self.list insertObject:@"新添加的行" atIndex:row];    [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];}

上面的代码中也可以不用insertRowsAtIndexPaths方法,而直接使用[tableView reloadData];语句,但是这样就没有添加的效果了。

      

刚运行时如上面左图所示,单击了某个加号后,新的一行就从右边进来了。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值