UITableView编辑

UITableView 编辑步骤如下:
1.让TableView处于编辑状态
2.协议设定
 2.1.确定Cell是否处于编辑状态
 2.2.设定Cell的编辑样式(删除、添加)

2.3.编辑状态进?提交

注意: 编辑结束后,由于numberOfRowsInSection这个协议只在 tableview添加到?视图的时候??次,?且table上的数据 都是由数组提供,因此,需要先将数组中的元素删除,然后 让table的协议重新??遍进?重新赋值。 即:先修改数据源,再刷新table(使?reloadData?法)

//每一个视图控制器都有一个编辑按钮,因为项目中编辑的应用场景非常多,所以系统预留了一个编辑按钮供我们使用

self.navigationItem.leftBarButtonItem = self.editButtonItem;

#pragma mark -----删除、添加数据-----

//1、让将要执行删除、添加操作的表视图处于编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    //先执行父类中的这个方法
    [super setEditing:editing animated:animated];
   
    //表视图执行此方法
    [self.tableView setEditing:editing animated:animated];
}

//2、指定表视图中哪些行可以处于编辑状态
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.row % 2 == 0) {
//        return YES;
//    }
//    return NO;
    //默认全部都可以进行编辑
    return YES;
}

//3、指定编辑样式,到底是删除还是添加
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    //如果此方法不重写,默认是删除样式
    return UITableViewCellEditingStyleInsert;
//    return UITableViewCellEditingStyleDelete;
//    if (indexPath.row > 10) {
//        return UITableViewCellEditingStyleInsert;
//    } else {
//        return UITableViewCellEditingStyleDelete;
//    }
}
//4、不管是删除还是添加,这个方法才是操作的核心方法,当点击删除、或者添加按钮时,需要做什么事情,怎样才能完成删除或者添加操作,全部在这个方法内部指定
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView beginUpdates];//表视图开始更新
   
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //1、将该位置下的单元格删除
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
       
        //2、删除数据数组中,与该单元格绑定的数据
        [_dataArray removeObjectAtIndex:indexPath.row];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        Student *student = _dataArray[indexPath.row];
       
        //获取一个位置信息
        NSIndexPath *index = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
        [tableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationTop];
        [_dataArray insertObject:student atIndex:index.row];
    }
   
    [tableView endUpdates];//表视图结束更新

}

#pragma mark -----表视图移动操作-----
//1、移动的第一步也是需要将表视图的编辑状态打开(上面已写)
//2、指定哪些行可以进行移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    //默认是都可以移动
    return YES;
}

//3、移动完成之后要做什么事情,怎么完成移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    //先记录原有位置下的模型数据
    Student *student = _dataArray[sourceIndexPath.row];
    [student retain];//防止野指针
    //删除原位置下的模型数据
    [_dataArray removeObjectAtIndex:sourceIndexPath.row];
    //在新位置将记录的模型数据添加到数据组中
    [_dataArray insertObject:student atIndex:destinationIndexPath.row];

[student release];

}

#pragma mark -----代理协议-----
//点击单元格触发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //push操作、传值
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    detailVC.student = _dataArray[indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];

}

····································

二、UITableViewController

UITableViewController是继承于UIViewController中的一个类,只不过比UIViewController中多了一个属性tableView。即:UITableViewController是一个自带table的视图控制器

self.view不是UIView而是UITableView

datasource和delegate默认都是self(UITableViewController)

开发中只需要建立UITableViewController子类

/*
 1、表视图控制器自带的根视图为UITableView对象
 2、self.tableView就代表根视图
 3、创建出来的表视图控制器 已经 自动接收了 数据源协议和代理协议
 4、不需要通过代码建立协议与代理关系
 5、表视图控制器.m文件已经自动帮你添加了数据源协议中必须实现的方法

*/

时间: 2025-01-14 20:35:49

UITableView编辑的相关文章

iOSDay30之UITableView编辑

1. UITableView编辑 1> UITableView 编辑流程 2> UITableView 编辑步骤(四步) ① 第一步 : 让 TableView 处于编辑状态(在按钮点击事件方法中) 1 // 优化写法 2 // 不带动画 3 _rootView.tableView.editing = !_rootView.tableView.editing; 4 // 带动画 5 [_rootView.tableView setEditing:!_rootView.tableView.edi

IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)

**********UiTableView编辑模式, 拖动位置 ,滑动删除 #import "HMViewController.h" @interface HMViewController () <UITableViewDataSource, UITableViewDelegate> /** 数据列表 */ @property (nonatomic, strong) NSMutableArray *dataList; @property (nonatomic, strong

UITableView 编辑

对 UITableView 进行添加,删除,移动,基本操作的流程. 1.初始化 UITableView 步骤: 1> 遵守协议 <UITableViewDelegate,UITableViewDataSource> 2> 设置代理 3> 实现方法 必须实现的方法有两个 - (NSInteger)tableView:numberOfRowsInSection:(设置每个分组的行数)- (UITableViewCell*)tableView: cellForRowAtIndexP

UITableView编辑模式

UITableView有两种模式,普通模式和编辑模式.在编辑模式下可以对cell进行排序.删除.插入等等. 如何进入编辑模式 调用tableView的setEditing(editing: Bool, animated: Bool)方法. 进入编辑模式以后发生了什么 向每个cell发送setEditing:animated:方法 进入编辑模式以后cell的变化 普通模式下cell的contentview的bounds就是cell的bounds. 编辑模式下,cell有三部分组成,左边的Editi

iOS - UITableView 编辑(cell的插入, 删除, 移动)

UITableView Cell的插入/删除 核心API Class : UITableView Delegate : UITableViewDataSource, UITableViewDelegate 涉及的API:(API的官方详细注释详见本章结尾) /** TableView 进入或退出编辑状态(TableView 方法). */ - (void)setEditing:(BOOL)editing animated:(BOOL)animate /** 确定哪些行的cell可以编辑 (UIT

UITableView 编辑模式(增加-删除-移动---自定义左滑 title)

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.dataArray = [NSMutableArray arrayWithArray: @[@"1",@"2",@"3",@"4",@"5",@"6"

UITableView编辑的实现原理和删除

一:执行过程 1,tableView进入编辑状态 用户点击一个按钮,让程序进入编辑状态, self.tableView.editing = YES; 2,询问tableView的cell能否编辑 tableView询问dataSource代理,让它执行一个代理方法询问每一行的编辑状态 即-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath //默认为yes,即在editin

UITableView编辑 增删改查

@interface RootTableViewController () @property (nonatomic , retain) NSMutableArray *dataArray; //用数组管理表视图将要显示的数据 @end @implementation RootTableViewController //新的重用机制格式 //1.定义全局区的静态重用标识符 static NSString *identifier = @"CELL"; //重写属性的getter方法 - 

第九章 UITableView编辑模式笔记

一,tableview自带编辑模式,可以添加.删除.移动item 二,可以添加section或者table的header和footer 三,使用interface Builder创建header的layout 四,UITableView显示header前,会向它的controller发送headerVIew消息 - (UIView *)headerView { // If you have not loaded the headerView yet... if (!_headerView) {