• editActionsForRowAtIndexPath(iOS8) tableview编辑(删除、插入、移动)


    ios8 出来的左滑小菜单 可以自定义想要的按钮 (要求ios8以上)

    - (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
     
        UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            
            NSLog(@"-----------=点击删除");
            
        }];
        
        
        UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"编辑" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            
            NSLog(@"-----------=点击编辑");
            
        }];
        
        UITableViewRowAction *topAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            
            
            NSLog(@"-----------=点击置顶");
            
        }];
        
        UITableViewRowAction *signAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"标记未读" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            
            NSLog(@"-----------=标记");
            
        }];
        
        // 毛玻璃效果
        deleteAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        
        editAction.backgroundColor = [UIColor blueColor];
        topAction.backgroundColor = [UIColor grayColor];
        signAction.backgroundColor = [UIColor yellowColor];
        
        return @[deleteAction, editAction, topAction, signAction];
    }

    可以在导航栏右边放编辑按钮,删除操作

    -(void)delete:(UIBarButtonItem *)sender {
        
        if (self.tableView.editing == NO) {
            self.tableView.editing = YES;
            sender.title = @"完成";
        }else if (self.tableView.editing == YES) {
            self.tableView.editing = NO;
            sender.title = @"编辑";
        }
        
    }
    
    // 设置tableView是否可以编辑
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
        return YES;
    }
    // 设置删除操作时候的标题
    -(NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
        return @"删除";
    }
    
     // 编辑模式
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
        return UITableViewCellEditingStyleDelete;
    }
    
    
    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
        
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            
            [self.arrayM removeObjectAtIndex:indexPath.row];
            
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
            
        }
        
    }

    导航栏右边放编辑按钮,插入操作

    // 设置tableView是否可以编辑
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
        return YES;
    }
    
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
        return UITableViewCellEditingStyleInsert;
    }
    
    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
        
        if (editingStyle == UITableViewCellEditingStyleInsert) {
            
            // 我们实现的是在所选行的位置插入一行,因此直接使用了参数indexPath
            NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];
            // 同样,将数据加到list中,用的row
            [self.arrayM insertObject:@"新添加的行" atIndex:indexPath.row];
            [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }

    导航栏右边放编辑按钮,移动操作

    // 设置编辑模式
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
        return UITableViewCellEditingStyleNone;
    }
    
    // 这个方法用来告诉表格 这一行是否可以移动
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
        return YES;
    }
    
    // 这个方法就是执行移动操作的
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *) sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
        
        NSUInteger fromRow = [sourceIndexPath row];
        NSUInteger toRow = [destinationIndexPath row];
        
        id object = [self.arrayM objectAtIndex:fromRow];
        [self.arrayM removeObjectAtIndex:fromRow];
        [self.arrayM insertObject:object atIndex:toRow];
    }
  • 相关阅读:
    付费客户端攻防篇
    psql查询结果保存文件
    mysql5.5自定义函数-计算并赋值
    TortoiseSVN 提交时出现错误 Error: Malformed svndiff data in representation
    打开文件共享突然提示:你没有权限访问\。请与网络管理员联系请求访问权限。
    golang 实现笛卡尔积
    爬虫练习3:提取全国省市县信息并统计
    爬虫练习2:爬取省市信息(增加地址信息)
    爬虫练习1:爬取省市信息
    个人使用的效率神器分享【不定期补充】
  • 原文地址:https://www.cnblogs.com/Mr-Ygs/p/5630275.html
Copyright © 2020-2023  润新知