iOS个人整理19-UITableViewController和UITableView的编辑

2024-05-11 21:32

本文主要是介绍iOS个人整理19-UITableViewController和UITableView的编辑,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、UITableViewController

UITableViewController是继承于UIViewController中的一个类,只不过比UIViewController中多了一个属性tableView。

也就是说UITableViewController是自带table的视图控制器。


它的self.view 是UITableView而不是UIView。

dataSource和delegate都默认是self。


总而言之,省了不少事情,相当于创建了一个UIViewController并在上面声明了完整的UITableView。



二、UITableView的编辑


对于UITableView来说,经常需要删除或者添加一条信息,比如,通讯录中的滑动删除和添加信息。

简单来说就是对单元格进行删除,添加,插入,以及调换顺序的操作。

这些操作主要通过各种代理方法来实现

1.让tableView处于编辑状态

//系统提供的方法按钮会调用此方法,让tableView处于编辑状态
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{//设置父类进入编辑状态[super setEditing:editing animated:animated];//开启表视图的编辑状态UITableView *tempTableView = [self.view viewWithTag:1000];[tempTableView setEditing:editing animated:animated];}//确定cell是否可以进入编辑状态,为NO就不能进入编辑状态
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{return YES;
}

2.设置编辑状态的style,决定插入或删除

//设置编辑的样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{//如果是最后一行,编辑模式为插入if (indexPath.row == _allDataMutableArray.count-1) {return UITableViewCellEditingStyleInsert;}elsereturn UITableViewCellEditingStyleDelete;
}

3.编辑状态的提交,对tableView进行改变

//编辑状态的提交
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{UITableView *tempTableView = [self.view viewWithTag:1000];//如果编辑模式为删除if (editingStyle == UITableViewCellEditingStyleDelete) {//先删除数据源,也就是数组中的内容[_allDataMutableArray removeObjectAtIndex:indexPath.row];//再删除单元格//这里要特别注意,必须先操作数据再操作单元格,不然崩的不要不要的[tempTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];}//如果编辑模式为插入if (editingStyle == UITableViewCellEditingStyleInsert) {//先插入到数组[_allDataMutableArray insertObject:@"新人" atIndex:indexPath.row];//再创建单元格[tempTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];}
}


下面是移动单元格的方法

1.首先还是要进入编辑状态,与上面第一步相同

2.设置是否可以移动

//实现协议,告诉tableView是否能移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{return YES;
}

3.检测移动的过程,可以对移动的源位置和目的位置进行限制

//监测移动过程,限制最后一行不能移动
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{//设置,如果要移动最后一行或者移动到最后一行,是不允许的if (sourceIndexPath.row == _allDataMutableArray.count - 1 || proposedDestinationIndexPath.row == _allDataMutableArray.count - 1) {return sourceIndexPath;}elsereturn proposedDestinationIndexPath;
}

4.进行移动的数据操作,如果不添加这一步,就没有修改数据源,重新加载后会复原

//移动
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{//先得到单元格上的数据NSString *tempStr = [self.allDataMutableArray objectAtIndex:sourceIndexPath.row];//把原位置的数据删除[_allDataMutableArray removeObjectAtIndex:sourceIndexPath.row];//把新数据添加到数组中对应的位置[_allDataMutableArray insertObject:tempStr atIndex:destinationIndexPath.row];}

5.让tableView重新加载数据

//刷新数据
-(void)refreshAction:(UIBarButtonItem*)sender
{UITableView *tempTableView = [self.view viewWithTag:1000];[tempTableView reloadData];
}



这里再贴上完整的代码和效果

//
#import "RootViewController.h"@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>@property (nonatomic,retain)NSMutableArray* allDataMutableArray;@end@implementation RootViewController- (void)viewDidLoad {[super viewDidLoad];//标题self.navigationItem.title = @"表视图的编辑";//添加右侧按钮//添加按钮UIBarButtonItem* addBarButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction:)];//编辑按钮UIBarButtonItem* editBarButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(editAction:)];//添加到右侧self.navigationItem.rightBarButtonItems = @[self.editButtonItem,addBarButton,editBarButton];//添加左侧刷新按钮self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshAction:)];//初始化UITableView *myTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];myTableView.tag = 1000;//代理myTableView.delegate = self;myTableView.dataSource = self;//添加到父视图[self.view addSubview:myTableView];//显示相关的属性//行高myTableView.rowHeight = 70;//分割线myTableView.separatorColor = [UIColor blueColor];myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//置顶视图myTableView.tableHeaderView = [[UIView alloc]init];//置底视图myTableView.tableFooterView = [[UIView alloc]init];//初始化数组数据self.allDataMutableArray = [[NSMutableArray alloc]initWithObjects:@"张三",@"李四",@"我擦",@"123",@"",@"111",@"333", nil];}//行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{return _allDataMutableArray.count;
}//cell填充
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//cell重用机制//定义重用标示符static NSString* cellId = @"CELL";//每次需要使用单元格的是,先根据重用标识符从重用队列中获取单元格,如果队列中没有,再进行初始化新的单元格//每次都会先创建一屏幕的cell,当有cell出屏幕,就会根据重用标识符添加到对应的重用队列中,当屏幕外的cell要进入屏幕,先从队列中获取,如果没有,则初始化cell//当重用cell时,需要对上面的控件程序赋值UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];//如果从重用队列中未获取cell,也就是Cell为空if (!cell) {cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];//系统提供的控件要配合不同的样式来使用
//        cell.detailTextLabel.text = [NSString stringWithFormat:@"创建的第%d个单元格",self.index++];}//UITableViewCell的属性//选中效果cell.selectionStyle = UITableViewCellSelectionStyleBlue;//辅助视图的样式cell.accessoryType = UITableViewCellAccessoryCheckmark;//设置左侧图片cell.imageView.image = [UIImage imageNamed:@"ck.jpg"];//标题视图cell.detailTextLabel.text = [NSString stringWithFormat:@"第%ld个cell,%ld个section",indexPath.row,indexPath.section];//副标题视图cell.textLabel.text = _allDataMutableArray[indexPath.row];//为相应位置返回定制好的单元格return cell;}
#pragma mark -- 按钮触发方法
//点击添加触发的方法
-(void)addAction:(UIBarButtonItem*)sender
{}
//进入编辑状态
-(void)editAction:(UIBarButtonItem*)sender
{[self setEditing:YES animated:NO];
}
//刷新数据
-(void)refreshAction:(UIBarButtonItem*)sender
{UITableView *tempTableView = [self.view viewWithTag:1000];[tempTableView reloadData];
}#pragma mark -- 编辑相关代理方法//系统提供的方法按钮会调用此方法,让tableView处于编辑状态
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{[super setEditing:editing animated:animated];//开启表视图的编辑状态UITableView *tempTableView = [self.view viewWithTag:1000];[tempTableView setEditing:editing animated:animated];}
//协议设定
//确定cell是否处于编辑状态,为NO就不能进入编辑状态
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{return YES;
}//编辑状态的提交
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{UITableView *tempTableView = [self.view viewWithTag:1000];//如果编辑模式为删除if (editingStyle == UITableViewCellEditingStyleDelete) {//先删除数据源,也就是数组中的内容[_allDataMutableArray removeObjectAtIndex:indexPath.row];//再删除单元格//这里要特别注意,必须先操作数据再操作单元格,不然崩的不要不要的[tempTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];}//如果编辑模式为插入if (editingStyle == UITableViewCellEditingStyleInsert) {//先插入到数组[_allDataMutableArray insertObject:@"新人" atIndex:indexPath.row];//再创建单元格[tempTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];}
}//设置编辑的样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{//如果是最后一行,编辑模式为插入if (indexPath.row == _allDataMutableArray.count-1) {return UITableViewCellEditingStyleInsert;}elsereturn UITableViewCellEditingStyleDelete;
}//移动相关
//实现协议,告诉tableView是否能移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{return YES;
}//移动
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{//先得到单元格上的数据NSString *tempStr = [self.allDataMutableArray objectAtIndex:sourceIndexPath.row];//把原位置的数据删除[_allDataMutableArray removeObjectAtIndex:sourceIndexPath.row];//把新数据添加到数组中对应的位置[_allDataMutableArray insertObject:tempStr atIndex:destinationIndexPath.row];}//监测移动过程,限制最后一行不能移动
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{if (sourceIndexPath.row == _allDataMutableArray.count - 1 || proposedDestinationIndexPath.row == _allDataMutableArray.count - 1) {return sourceIndexPath;}elsereturn proposedDestinationIndexPath;
}@end

效果如下

 



这篇关于iOS个人整理19-UITableViewController和UITableView的编辑的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/980706

相关文章

Mysql中深分页的五种常用方法整理

《Mysql中深分页的五种常用方法整理》在数据量非常大的情况下,深分页查询则变得很常见,这篇文章为大家整理了5个常用的方法,文中的示例代码讲解详细,大家可以根据自己的需求进行选择... 目录方案一:延迟关联 (Deferred Join)方案二:有序唯一键分页 (Cursor-based Paginatio

使用DeepSeek搭建个人知识库(在笔记本电脑上)

《使用DeepSeek搭建个人知识库(在笔记本电脑上)》本文介绍了如何在笔记本电脑上使用DeepSeek和开源工具搭建个人知识库,通过安装DeepSeek和RAGFlow,并使用CherryStudi... 目录部署环境软件清单安装DeepSeek安装Cherry Studio安装RAGFlow设置知识库总

Mysql中InnoDB与MyISAM索引差异详解(最新整理)

《Mysql中InnoDB与MyISAM索引差异详解(最新整理)》InnoDB和MyISAM在索引实现和特性上有差异,包括聚集索引、非聚集索引、事务支持、并发控制、覆盖索引、主键约束、外键支持和物理存... 目录1. 索引类型与数据存储方式InnoDBMyISAM2. 事务与并发控制InnoDBMyISAM

StarRocks索引详解(最新整理)

《StarRocks索引详解(最新整理)》StarRocks支持多种索引类型,包括主键索引、前缀索引、Bitmap索引和Bloomfilter索引,这些索引类型适用于不同场景,如唯一性约束、减少索引空... 目录1. 主键索引(Primary Key Index)2. 前缀索引(Prefix Index /

如何解决Pycharm编辑内容时有光标的问题

《如何解决Pycharm编辑内容时有光标的问题》文章介绍了如何在PyCharm中配置VimEmulator插件,包括检查插件是否已安装、下载插件以及安装IdeaVim插件的步骤... 目录Pycharm编辑内容时有光标1.如果Vim Emulator前面有对勾2.www.chinasem.cn如果tools工

详解Spring Boot接收参数的19种方式

《详解SpringBoot接收参数的19种方式》SpringBoot提供了多种注解来接收不同类型的参数,本文给大家介绍SpringBoot接收参数的19种方式,感兴趣的朋友跟随小编一起看看吧... 目录SpringBoot接受参数相关@PathVariable注解@RequestHeader注解@Reque

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

数论入门整理(updating)

一、gcd lcm 基础中的基础,一般用来处理计算第一步什么的,分数化简之类。 LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; } <pre name="code" class="cpp">LL lcm(LL a, LL b){LL c = gcd(a, b);return a / c * b;} 例题:

【iOS】MVC模式

MVC模式 MVC模式MVC模式demo MVC模式 MVC模式全称为model(模型)view(视图)controller(控制器),他分为三个不同的层分别负责不同的职责。 View:该层用于存放视图,该层中我们可以对页面及控件进行布局。Model:模型一般都拥有很好的可复用性,在该层中,我们可以统一管理一些数据。Controlller:该层充当一个CPU的功能,即该应用程序

HomeBank:开源免费的个人财务管理软件

在个人财务管理领域,找到一个既免费又开源的解决方案并非易事。HomeBank&nbsp;正是这样一个项目,它不仅提供了强大的功能,还拥有一个活跃的社区,不断推动其发展和完善。 开源免费:HomeBank 是一个完全开源的项目,用户可以自由地使用、修改和分发。用户友好的界面:提供直观的图形用户界面,使得非技术用户也能轻松上手。数据导入支持:支持从 Quicken、Microsoft Money