zoukankan      html  css  js  c++  java
  • 表格编辑

    1、设置表格编辑开关状态

    // 设置表格的编辑状态
    myTableView.editing = YES;
    
    // 翻转表格的编辑状态
    myTableView.editing = !myTableView.editing;
    
    // 带动画翻转表格的编辑状态
    [myTableView setEditing:!myTableView.editing animated:YES];
    

    2、修改左滑删除按钮的内容

    // UITableViewDelegate 协议方法
    /*
    默认为 Delete
    */
    - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {   
    	return @"删除";
    }
    

    3、设置左滑多按钮

    - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    	UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal
    	title:@"关注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
    		NSLog(@"点击了关注");
    		// 收回左滑出现的按钮(退出编辑模式)
    		tableView.editing = NO;
    	}];
    
    	UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
    	title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
    		[[myDataArray objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
    		[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    	}];
    
    	// 按钮从右向左的顺序排列
    	return @[action1, action0];
    }
    

    4、设置编辑模式

    /*
    UITableViewCellEditingStyleNone;        // 无
    UITableViewCellEditingStyleDelete;      // 删除模式,默认
    UITableViewCellEditingStyleInsert;      // 插入模式
    UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;   // 多选模式
    */
    
    // UITableViewDelegate 协议方法
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    	// 删除、插入、多选删除,不设置默认时为删除
    	if (0 == indexPath.section) {
    		return UITableViewCellEditingStyleDelete;
    	}
    	else {
    		return UITableViewCellEditingStyleInsert;
    	}
    }
    

    5、表格删除、插入

    • 5.1 表格删除:

      • 5.1.1 先将数据从数据源里删除,
      • 5.1.2 刷新显示
      • 再从 tableView 里删除 cell:
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
      
      • 或者再直接重载整个表格:
      [tableView reloadData];
      
      • 或者在直接重载分段:
      [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
      
    • 5.2 表格插入:

      • 5.2.1 先将数据插入到数据源中,
      • 5.2.2 刷新显示
        • 再插入一个 cell:
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        
        • 或者再直接重载整个表格:
        [tableView reloadData];
        
        • 或者在直接重载分段:
        [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
        
      • 5.2.3 实际代码
      // 表格删除或插入,默认为删除模式,写入该方法即表示允许删除
      
      // UITableViewDataSource 协议方法
      - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
      
      	// 判断编辑风格,默认是删除
      	if (editingStyle == UITableViewCellEditingStyleDelete) {
      
      			// 表格删除
      			// 从数据源里删除
      			[[myDataArray objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
      
      			// 从 tableView 里删除 cell
      			[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
      	}
      	else if (editingStyle == UITableViewCellEditingStyleInsert) {
      
      		Person *person = [[Person alloc] init];                 
      		person.name = @"xiao bai";
      		person.age = 18;
      
      		// 表格插入
      		// 插入到数据源中
      		[[myDataArray objectAtIndex:indexPath.section] insertObject:person atIndex:indexPath.row];
      
      		// 插入一个 cell
      		[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
      	}
      }
      

    6、表格移动

    • 6.1 原理介绍

      • 1.先在数据源中找到需要移动的对象。
      • 2.然后在数据源数组中从原始位置删掉。
      • 3.再在数据源数组中插入到新位置。
      • 4.最后重新加载表格:
      [tableView reloadData];
      
      • 或者在直接重载分段:
      [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
      
    • 6.2 实际代码

    // 写入该方法即表示允许移动
    // UITableViewDataSource 协议方法
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    
    	// 找到需要移动的对象
    	Person *person = [[myDataArray objectAtIndex:sourceIndexPath.section] objectAtIndex:sourceIndexPath.row];
    
    	// 从原始位置删掉
    	[[myDataArray objectAtIndex:sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
    
    	// 插入到新位置
    	[[myDataArray objectAtIndex:destinationIndexPath.section] insertObject:person atIndex:destinationIndexPath.row];
    
    	// 刷新 tableView
    	[tableView reloadData];
    }
    
  • 相关阅读:
    Java图片裁剪
    jvm参数
    Druid数据源监控配置
    执行jar包或执行其中的某个类
    十进制和二进制之间的相互转化
    Java位运算
    获取网络资源保存本地
    前端PHP入门-010-内部函数
    前端PHP入门-011-可变函数
    前端PHP入门-009-匿名函数
  • 原文地址:https://www.cnblogs.com/CH520/p/9420423.html
Copyright © 2011-2022 走看看