UITableView中的各种小用法

本文详细介绍如何在iOS应用中实现表格视图的标记、移动和删除行等功能,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、标记行

 

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


为了实现标记功能,在ViewController.m中@end之前添加代码:

  1. #pragma mark -   
  2. #pragma mark Table Delegate Methods   
  3. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {   
  4.     UITableViewCell *oneCell = [tableView cellForRowAtIndexPath: indexPath];  
  5.     if (oneCell.accessoryType == UITableViewCellAccessoryNone) {  
  6.         oneCell.accessoryType = UITableViewCellAccessoryCheckmark;  
  7.     } else   
  8.         oneCell.accessoryType = UITableViewCellAccessoryNone;  
  9.     [tableView deselectRowAtIndexPath:indexPath animated:YES];   
  10. }  
#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]; 
}

该代码实现:单击某行时,若此行未被标记,则标记此行;若此行已经被标记,则取消标记。

运行效果如上图。

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

UITableViewCellAccessoryCheckmark
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryDisclosureIndicator
UITableViewCellAccessoryNone

       

   UITableViewCellAccessoryCheckmark                   UITableViewCellAccessoryDetailDisclosureButton

            

     UITableViewCellAccessoryDisclosureIndicator           UITableViewCellAccessoryNone

2、移动行

 

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

 

2.1 打开ViewController.xib,将其中的表格控件映射成Outlet到ViewController.h,名称为myTableView。

2.2 打开ViewController.m,在viewDidLoad方法最后添加代码:

  1. //启动表格的编辑模式   
  2. [self.myTableView setEditing:YES animated:YES];  
//启动表格的编辑模式
[self.myTableView setEditing:YES animated:YES];
2.3 在@end之前添加代码:

  1. //打开编辑模式后,默认情况下每行左边会出现红的删除按钮,这个方法就是关闭这些按钮的   
  2. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView  
  3.            editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {   
  4.     return UITableViewCellEditingStyleNone;   
  5. }   
  6.   
  7. //这个方法用来告诉表格 这一行是否可以移动   
  8. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {   
  9.     return YES;   
  10. }  
  11.   
  12. //这个方法就是执行移动操作的   
  13. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)  
  14.         sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {  
  15.     NSUInteger fromRow = [sourceIndexPath row];   
  16.     NSUInteger toRow = [destinationIndexPath row];   
  17.       
  18.     id object = [list objectAtIndex:fromRow];   
  19.     [list removeObjectAtIndex:fromRow];   
  20.     [list insertObject:object atIndex:toRow];   
  21. }  
//打开编辑模式后,默认情况下每行左边会出现红的删除按钮,这个方法就是关闭这些按钮的
- (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,它表示不可编辑,这里的编辑指的是删除和插入。表示表格行的编辑模式的常量有:

  1. UITableViewCellEditingStyleDelete  
  2. UITableViewCellEditingStyleInsert  
  3. UITableViewCellEditingStyleNone  
UITableViewCellEditingStyleDelete
UITableViewCellEditingStyleInsert
UITableViewCellEditingStyleNone

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

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


2.4 运行,从下图可以看到实现了行的移动:


但是也会发现,现在无法对每行进行标记了。这说明,在编辑模式下,无法选择行,从而didSelectRowAtIndexPath这个方法不会执行。

3、删除行

 

从第2步过来,实现删除某行,其实比较简单了。

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

3.2 在@end之前添加代码:

  1. //这个方法根据参数editingStyle是UITableViewCellEditingStyleDelete   
  2. //还是UITableViewCellEditingStyleDelete执行删除或者插入   
  3. - (void)tableView:(UITableView *)tableView commitEditingStyle:  
  4.     (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {  
  5.     if (editingStyle == UITableViewCellEditingStyleDelete) {  
  6.         NSUInteger row = [indexPath row];   
  7.         [self.list removeObjectAtIndex:row];   
  8.         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  
  9.                          withRowAnimation:UITableViewRowAnimationAutomatic];   
  10.     }  
  11. }  
//这个方法根据参数editingStyle是UITableViewCellEditingStyleDelete
//还是UITableViewCellEditingStyleDelete执行删除或者插入
- (void)tableView:(UITableView *)tableView commitEditingStyle:
    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSUInteger row = [indexPath row]; 
        [self.list removeObjectAtIndex:row]; 
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationAutomatic]; 
    }
}
在这个方法中又出现了一个常量:UITableViewRowAnimationAutomatic,它表示删除时的效果,类似的常量还有:

  1. UITableViewRowAnimationAutomatic  
  2. UITableViewRowAnimationTop  
  3. UITableViewRowAnimationBottom  
  4. UITableViewRowAnimationLeft  
  5. UITableViewRowAnimationRight  
  6. UITableViewRowAnimationMiddle  
  7. UITableViewRowAnimationFade  
  8. UITableViewRowAnimationNone  
UITableViewRowAnimationAutomatic
UITableViewRowAnimationTop
UITableViewRowAnimationBottom
UITableViewRowAnimationLeft
UITableViewRowAnimationRight
UITableViewRowAnimationMiddle
UITableViewRowAnimationFade
UITableViewRowAnimationNone
它们的效果就不一一介绍了,可以在实际使用时试试。


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

4、插入行

 

这个与删除行类似。

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

4.2在3.2添加的方法中添加代码:

  1. else {  
  2.     //我们实现的是在所选行的位置插入一行,因此直接使用了参数indexPath   
  3.     NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];  
  4.     //同样,将数据加到list中,用的row   
  5.     [self.list insertObject:@"新添加的行" atIndex:row];  
  6.     [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];  
  7. }  
else {
    //我们实现的是在所选行的位置插入一行,因此直接使用了参数indexPath
    NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];
    //同样,将数据加到list中,用的row
    [self.list insertObject:@"新添加的行" atIndex:row];
    [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
}
上面的代码中也可以不用insertRowsAtIndexPaths方法,而直接使用[tableView reloadData];语句,但是这样就没有添加的效果了。

4.3 好了,运行一下:


刚运行时如上面左图所示,单击了某个加号后,新的一行就从右边飞进来了,因为在insertRowsAtIndexPaths中用了参数UITableViewRowAnimationRight。


转自:http://justcoding.iteye.com/blog/1485768

内容概要:本文介绍了多种开发者工具及其对开发效率的提升作用。首先,介绍了两款集成开发环境(IDE):IntelliJ IDEA 以其智能代码补全、强大的调试工具和项目管理功能适用于Java开发者;VS Code 则凭借轻量级和多种编程语言的插件支持成为前端开发者的常用工具。其次,提到了基于 GPT-4 的智能代码生成工具 Cursor,它通过对话式编程显著提高了开发效率。接着,阐述了版本控制系统 Git 的重要性,包括记录代码修改、分支管理和协作功能。然后,介绍了 Postman 作为 API 全生命周期管理工具,可创建、测试和文档化 API,缩短前后端联调时间。再者,提到 SonarQube 这款代码质量管理工具,能自动扫描代码并检测潜在的质量问题。还介绍了 Docker 容器化工具,通过定义应用的运行环境和依赖,确保环境一致性。最后,提及了线上诊断工具 Arthas 和性能调优工具 JProfiler,分别用于生产环境排障和性能优化。 适合人群:所有希望提高开发效率的程序员,尤其是有一定开发经验的软件工程师和技术团队。 使用场景及目标:①选择合适的 IDE 提升编码速度和代码质量;②利用 AI 编程助手加快开发进程;③通过 Git 实现高效的版本控制和团队协作;④使用 Postman 管理 API 的全生命周期;⑤借助 SonarQube 提高代码质量;⑥采用 Docker 实现环境一致性;⑦运用 Arthas 和 JProfiler 进行线上诊断和性能调优。 阅读建议:根据个人或团队的需求选择适合的工具,深入理解每种工具的功能特点,并在实际开发中不断实践和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值