iOS之tableView(五)的编辑删除插入操作和UIAlertController的使用

本文主要是介绍iOS之tableView(五)的编辑删除插入操作和UIAlertController的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

- (NSMutableArray *)dataArray {if (nil ==_dataArray) {// 实例化 dataArray#warning 不要忘记实例化 dataArray数组_dataArray = [NSMutableArrayarray];// 1. 路径NSString *path = [[NSBundlemainBundle]pathForResource:@"heros.plist"ofType:nil];// 2. 读取内容NSArray *tempArray = [NSArrayarrayWithContentsOfFile:path];// 3. 可变数组//        NSMutableArray *mutable = [NSMutableArray array];// 4. 字典转模型for (NSDictionary *dictin tempArray) {HeroModel *model = [HeroModelheroModelWithDict:dict];[_dataArray addObject:model];}//        _dataArray = mutable;}return_dataArray;}- (void)viewDidLoad {[superviewDidLoad];// 设置控制器成为tableView的代理_tableView.delegate =self;// 打开tableView的编辑模式_tableView.editing = YES; ++++++++++++++++++++++++++++++++// 如果想实现,滑动删除,就必须把这个属性给关闭_tableView.editing = YES;+++++++++++++++++++++++++++++++++}// 多少组- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1;}// 多少行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.dataArray.count;}// 每一行显示的内容- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {// static 避免多次分配内存static NSString *identifier =@"fdsfdsfds";// 1. 到缓存池中去找cellUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];// 2. 判断是否取到,如果取不到就实例化新的cellif (nil == cell) {// 实例化tableViewcellcell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:identifier];}// 3. 设置cell上控件的数据// 设置cell上控件的内容HeroModel *model = self.dataArray[indexPath.row];// 设置imageViewcell.imageView.image = [UIImageimageNamed:model.icon];// 设置文本cell.textLabel.text = model.name;// 设置detailTextLabelcell.detailTextLabel.text = model.intro;// 设置右侧箭头// accessory : 配件cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;return cell;}// 决定那一行是可以被编辑的;如果不实现这个方法默认每一行都可以编辑- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {return YES;}// 点击delete的时候会调用这个方法/**UITableViewCellEditingStyleNone,UITableViewCellEditingStyleDelete,UITableViewCellEditingStyleInsert*/- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {// 可以对传入的编辑模式进行判断if (editingStyle ==UITableViewCellEditingStyleDelete) {// 1. 在数据源中把row 对应到 dataArray下标中的对象给移除掉[self.dataArrayremoveObjectAtIndex:indexPath.row];// 2. 刷新数据//        [_tableView reloadData];//        [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];[_tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];} elseif (editingStyle ==UITableViewCellEditingStyleInsert) {// 1. 根据点击的indexPath.row 在数据源中插入heroModel 对象HeroModel *model = [[HeroModelalloc]init];model.name = @"千珏";[self.dataArrayinsertObject:modelatIndex:indexPath.row];// 2. 刷新数据[_tableViewinsertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft ];}}#pragma mark -#pragma mark -  当一行cell被取消选中的时候会调用- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {NSLog(@"调用了--- %ld", indexPath.row);}#pragma mark -#pragma mark -  当cell被选中的时候调用- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}#pragma mark -#pragma mark -  决定删除按钮上面显示的文本- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {return @"咔嚓掉";}#pragma mark -#pragma mark -  决定编辑模式,如果不实现者个方法,默认是删除模式- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {returnUITableViewCellEditingStyleDelete;}- (BOOL)prefersStatusBarHidden {return YES;}=============cell滑动删除时显示多个按钮======#pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{// 添加一个删除按钮UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {NSLog(@"点击了删除");}];// 删除一个置顶按钮UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {NSLog(@"点击了置顶");}];topRowAction.backgroundColor = [UIColor blueColor];// 添加一个更多按钮UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {NSLog(@"点击了更多");}];moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];// 将设置好的按钮放到数组中返回return @[deleteRowAction, topRowAction, moreRowAction];}

 

 

***删除cell方法一:


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {return YES;
}// 定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {return UITableViewCellEditingStyleDelete;
}// 进入编辑模式,按下出现的编辑按钮后,进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {if (editingStyle == UITableViewCellEditingStyleDelete) {}
}// 修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {return @"删除";
}

 

 

*****删除cell方法二:

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
// 添加一个删除按钮
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {NSLog(@"点击了删除");
}];// 删除一个置顶按钮UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {NSLog(@"点击了置顶");}];topRowAction.backgroundColor = [UIColor blueColor];// 添加一个更多按钮UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {NSLog(@"点击了更多");
}];moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];// 将设置好的按钮放到数组中返回return @[deleteRowAction, topRowAction, moreRowAction];}

*******删除cell方法三:

//iOS11后的删除方法
-(UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
API_AVAILABLE(ios(11.0)){UISwipeActionsConfiguration *config;if (@available(iOS 11.0, *)) {UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {}];deleteRowAction.image = [UIImage imageNamed:@"删除"];deleteRowAction.backgroundColor = [UIColor redColor];config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];} else {}return config;}

 

这篇关于iOS之tableView(五)的编辑删除插入操作和UIAlertController的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.