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

相关文章

使用Python实现一键隐藏屏幕并锁定输入

《使用Python实现一键隐藏屏幕并锁定输入》本文主要介绍了使用Python编写一个一键隐藏屏幕并锁定输入的黑科技程序,能够在指定热键触发后立即遮挡屏幕,并禁止一切键盘鼠标输入,这样就再也不用担心自己... 目录1. 概述2. 功能亮点3.代码实现4.使用方法5. 展示效果6. 代码优化与拓展7. 总结1.

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Linux中的计划任务(crontab)使用方式

《Linux中的计划任务(crontab)使用方式》:本文主要介绍Linux中的计划任务(crontab)使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、前言1、linux的起源与发展2、什么是计划任务(crontab)二、crontab基础1、cro

kotlin中const 和val的区别及使用场景分析

《kotlin中const和val的区别及使用场景分析》在Kotlin中,const和val都是用来声明常量的,但它们的使用场景和功能有所不同,下面给大家介绍kotlin中const和val的区别,... 目录kotlin中const 和val的区别1. val:2. const:二 代码示例1 Java

C++变换迭代器使用方法小结

《C++变换迭代器使用方法小结》本文主要介绍了C++变换迭代器使用方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、源码2、代码解析代码解析:transform_iterator1. transform_iterat

C++中std::distance使用方法示例

《C++中std::distance使用方法示例》std::distance是C++标准库中的一个函数,用于计算两个迭代器之间的距离,本文主要介绍了C++中std::distance使用方法示例,具... 目录语法使用方式解释示例输出:其他说明:总结std::distance&n编程bsp;是 C++ 标准

vue使用docxtemplater导出word

《vue使用docxtemplater导出word》docxtemplater是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtempl... 目录docxtemplatervue使用docxtemplater导出word安装常用语法 封装导出方

Linux换行符的使用方法详解

《Linux换行符的使用方法详解》本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的... 目录简介检测文件中的换行符使用 cat -A 查看换行符使用 od -c 检查字符换行符格式转换将

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.