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

相关文章

SpringBoot使用ffmpeg实现视频压缩

《SpringBoot使用ffmpeg实现视频压缩》FFmpeg是一个开源的跨平台多媒体处理工具集,用于录制,转换,编辑和流式传输音频和视频,本文将使用ffmpeg实现视频压缩功能,有需要的可以参考... 目录核心功能1.格式转换2.编解码3.音视频处理4.流媒体支持5.滤镜(Filter)安装配置linu

Redis中的Lettuce使用详解

《Redis中的Lettuce使用详解》Lettuce是一个高级的、线程安全的Redis客户端,用于与Redis数据库交互,Lettuce是一个功能强大、使用方便的Redis客户端,适用于各种规模的J... 目录简介特点连接池连接池特点连接池管理连接池优势连接池配置参数监控常用监控工具通过JMX监控通过Pr

解决mysql插入数据锁等待超时报错:Lock wait timeout exceeded;try restarting transaction

《解决mysql插入数据锁等待超时报错:Lockwaittimeoutexceeded;tryrestartingtransaction》:本文主要介绍解决mysql插入数据锁等待超时报... 目录报错信息解决办法1、数据库中执行如下sql2、再到 INNODB_TRX 事务表中查看总结报错信息Lock

apache的commons-pool2原理与使用实践记录

《apache的commons-pool2原理与使用实践记录》ApacheCommonsPool2是一个高效的对象池化框架,通过复用昂贵资源(如数据库连接、线程、网络连接)优化系统性能,这篇文章主... 目录一、核心原理与组件二、使用步骤详解(以数据库连接池为例)三、高级配置与优化四、典型应用场景五、注意事

使用Python实现Windows系统垃圾清理

《使用Python实现Windows系统垃圾清理》Windows自带的磁盘清理工具功能有限,无法深度清理各类垃圾文件,所以本文为大家介绍了如何使用Python+PyQt5开发一个Windows系统垃圾... 目录一、开发背景与工具概述1.1 为什么需要专业清理工具1.2 工具设计理念二、工具核心功能解析2.

Linux系统之stress-ng测压工具的使用

《Linux系统之stress-ng测压工具的使用》:本文主要介绍Linux系统之stress-ng测压工具的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、理论1.stress工具简介与安装2.语法及参数3.具体安装二、实验1.运行8 cpu, 4 fo

Java使用MethodHandle来替代反射,提高性能问题

《Java使用MethodHandle来替代反射,提高性能问题》:本文主要介绍Java使用MethodHandle来替代反射,提高性能问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录一、认识MethodHandle1、简介2、使用方式3、与反射的区别二、示例1、基本使用2、(重要)

使用C#删除Excel表格中的重复行数据的代码详解

《使用C#删除Excel表格中的重复行数据的代码详解》重复行是指在Excel表格中完全相同的多行数据,删除这些重复行至关重要,因为它们不仅会干扰数据分析,还可能导致错误的决策和结论,所以本文给大家介绍... 目录简介使用工具C# 删除Excel工作表中的重复行语法工作原理实现代码C# 删除指定Excel单元

MySQL 事务的概念及ACID属性和使用详解

《MySQL事务的概念及ACID属性和使用详解》MySQL通过多线程实现存储工作,因此在并发访问场景中,事务确保了数据操作的一致性和可靠性,下面通过本文给大家介绍MySQL事务的概念及ACID属性和... 目录一、什么是事务二、事务的属性及使用2.1 事务的 ACID 属性2.2 为什么存在事务2.3 事务

使用Python实现网页表格转换为markdown

《使用Python实现网页表格转换为markdown》在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,本文将使用Python编写一个网页表格转Markdown工具,需... 在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,以便在文档、邮件或