博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS之tableView(五)的编辑删除插入操作和UIAlertController的使用
阅读量:4289 次
发布时间:2019-05-27

本文共 7983 字,大约阅读时间需要 26 分钟。

- (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. 到缓存池中去找cell    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];        // 2. 判断是否取到,如果取不到就实例化新的cell    if (nil == cell) {        // 实例化tableViewcell        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:identifier];    }            // 3. 设置cell上控件的数据    // 设置cell上控件的内容    HeroModel *model = self.dataArray[indexPath.row];        // 设置imageView    cell.imageView.image = [UIImageimageNamed:model.icon];        // 设置文本    cell.textLabel.text = model.name;        // 设置detailTextLabel    cell.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 *)indexPathAPI_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;    }

 

转载地址:http://oqlgi.baihongyu.com/

你可能感兴趣的文章
Android指纹识别深入浅出分析到实战
查看>>
你们要的多数据库功能终于来了
查看>>
Android中实现微信本地视频发布到朋友圈功能
查看>>
非替代品,MongoDB与MySQL对比分析
查看>>
Hadoop平台相关技术
查看>>
java学习11天-自定义异常&异常转换(实例应用)
查看>>
MySql、SqlServer、Oracle数据库行转列大全
查看>>
程序员常用的自助建站资源汇总!
查看>>
分布式与集群的区别是什么?
查看>>
MySql常用必备脚本大全
查看>>
Velocity初探小结--velocity使用语法详解
查看>>
设计模式学习 - Singleton Pattern
查看>>
学习Spring——依赖注入
查看>>
CSS3 transform 属性详解
查看>>
Java对象内存结构及大小计算
查看>>
Spring MVC注解的一些案列
查看>>
Web大文件断点续传,快来看看吧!
查看>>
javascript高级编程3第二章:在html中使用javascript
查看>>
Android中热修复框架AndFix原理解析及案例使用
查看>>
手写代码实现EventBus
查看>>