tableView的编辑

首先记住声明编辑样式的属性  UITableViewCellEditingStyle 和四个步骤

第一步:让tableView处于编辑状态

[self.rootView.tabView setEditing:!self.rootView.tabView.editing animated:YES];

第二步:指定哪些cell可以被编辑。 默认是所有的cell都可以被编辑,(但是这个方法可以指定哪个分区可以被编辑)

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

第三步:设置编辑样式 (默认删除)

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.style;
}

第四步:完成编辑, 提交编辑状态

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

ViewController.m

#import "RootViewController.h"
#import "RootView.h"
#define kColur arc4random() % 256 / 255.0
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)RootView *rootView;

// 声名大数组,用来存放所有的学生信息
@property (nonatomic, strong)NSMutableArray *AllDataArrray;

// 声明编辑样式的属性
@property (nonatomic, assign)UITableViewCellEditingStyle style;

@end

@implementation RootViewController

- (void)loadView {
    self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.rootView.tabView.dataSource = self;
    self.rootView.tabView.delegate = self;
    // 设置数据
    [self handleDate];

    // 处理导航栏
    self.title = @"管理";
    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
    // 添加右按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(click:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(leftClick:)];
}

// 设置数据

- (void)handleDate {
    // 1.初始化大数组
    self.AllDataArrray = [NSMutableArray array];

    // 2.定义三个数组存放每一组学生的姓名
    NSMutableArray *array1 = @[@"-1", @"ququ", @"网红", @"老司机", @"bobo", @"唱吧网红小尊"].mutableCopy;
    NSMutableArray *array2 = @[@"yida", @"mbBoy", @"lida", @"boomSky", @"正能量"].mutableCopy;
    NSMutableArray *array3 = @[@"py", @"她家傲然", @"MBBoy", @"??神", @"雷神"].mutableCopy;

    // 将所有学生存放大数组中
    [self.AllDataArrray addObject:array1];
    [self.AllDataArrray addObject:array2];
    [self.AllDataArrray addObject:array3];

}

// 设置分组个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.AllDataArrray.count;
}

// 设置每一个分区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.AllDataArrray[section] count];
}

// 返回cell对象
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 1.从重用池查找cell
    static NSString *identifire = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifire];
    // 2.判断,如果没有招待创建cell对象
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifire];
    }

    // 3.设置cell数据
    NSArray *array = self.AllDataArrray[indexPath.section];
    cell.textLabel.text = array[indexPath.row];

    cell.backgroundColor = [UIColor colorWithRed:kColur green:kColur blue:kColur alpha:1];

    // 4.返回cell对象

    // 点击效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

// 返回每一行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark UITabView 编辑---

/*******************************************************************/

// 左按钮点击方法

- (void)leftClick:(UIBarButtonItem *)sender {

    // 左按钮进行插入
    self.style = UITableViewCellEditingStyleInsert;

    // 让tabView处于编辑状态
    [self.rootView.tabView setEditing:!self.rootView.tabView.editing animated:YES];
}

// 实现右按钮点击方法

- (void)click:(UIBarButtonItem *)sender {

    // 第一步:让tableView处于编辑状态
//    if (self.rootView.tabView.editing) {
//        self.rootView.tabView.editing = NO;
//    } else {
//        self.rootView.tabView.editing = YES;
//    }

    self.style = UITableViewCellEditingStyleDelete;
    // 优化写法
    [self.rootView.tabView setEditing:!self.rootView.tabView.editing animated:YES];

}

// 第二步:指定哪些cell可以被编辑。 默认是所有的cell都可以被编辑,(但是这个方法可以指定哪个分区可以被编辑)

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

// 第三步:设置编辑样式 (默认删除)

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.style;
}

// 第四步:完成编辑, 提交编辑状态

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    // 判断编辑样式
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 1.删除数据
        [self.AllDataArrray[indexPath.section] removeObjectAtIndex:indexPath.row];
        // 2.更新UI
        // 删除一行更新页面
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];

        // 全部更新
        //    [tableView reloadData];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {

        // 1.添加数据到数组中
        [self.AllDataArrray[indexPath.section] insertObject:@"你是不是傻?" atIndex:indexPath.row + 1];
        // 2.更新UI
//        [tableView reloadData];
        // 更新一行

        // 创建新一行的NSindexPath对象
        NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];

        [tableView insertRowsAtIndexPaths: @[newIndexPath] withRowAnimation:UITableViewRowAnimationBottom];

    }

}

tableViewCell移动
 前面的步骤一样只是后最后一步时用得方法

开始移动

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {

    // 1. 获取需要修改的数据
    NSString *sourceName = [self.allDataArray[sourceIndexPath.section] objectAtIndex:sourceIndexPath.row];

    // 2.先将数据从当前数组中移除
    [self.allDataArray[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];

    // 3.将数据插入到对应位置
    [self.allDataArray[destinationIndexPath.section] insertObject:sourceName atIndex:destinationIndexPath.row];

}

防止随意移动

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    } else {
        return sourceIndexPath;
    }
}
时间: 2024-08-28 15:26:43

tableView的编辑的相关文章

ios之UITableViewController(二) tableView的编辑模式

tableView的编辑模式 表视图可以进入编辑模式,当进入编辑模式就可以进行删除.插入.移动单元等操作 效果图: 让表视图进入编辑模式,进入编辑模式的方法有两种,一种是使用导航栏的edit 按钮,另一种是设置tableView的editing属性进入编辑模式. 最后通过实现UITableViewDataSource协议的方法实现单元格的删除.插入和移动 1,在viewDidLoad方法里面指定导航栏的右按钮为edit按钮 self.navigationItem.rightBarButtonIt

iOS UIKit:TableView之编辑模式(3)

一般table view有编辑模式和正常模式,当table view进入编辑模式时,会在row的左边显示编辑和重排控件,如图 42所示的编辑模式时的控件布局:左边的editing control有表 61的两种图标. 表 61 table view编辑控件 图标 描述 Deletion控件 Insertion控件 若table view在编辑模式时,用户点击编辑控件,那么table view会发送一系列消息给data source 和delegate对象.可以通过实现这些方法来修改table v

iOS开发UI篇-tableView在编辑状态下的批量操作(多选)

先看下效果图 直接上代码 #import "MyController.h" @interface MyController () { UIButton *button; } @property(nonatomic,strong)NSMutableArray *array;//数据源 @property (nonatomic,strong)NSMutableArray *selectorPatnArray;//存放选中数据 @end @implementation MyControlle

UI-关于tableView编辑 tableView移动 的代码理解

1 #import "ViewController.h" 2 3 #pragma mark- 编辑的步骤 4 //UITableView编辑步骤 5 @interface ViewController ()<UITableViewDelegate, UITableViewDataSource> 6 {//第一步;UITableView有两种编辑状态 默认是删除状态,所以我们要一个容器 保存我们想要的状态 7 UITableViewCellEditingStyle _edit

UITableView编辑

UITableView 编辑步骤如下: 1.让TableView处于编辑状态 2.协议设定  2.1.确定Cell是否处于编辑状态  2.2.设定Cell的编辑样式(删除.添加) 2.3.编辑状态进?提交 注意: 编辑结束后,由于numberOfRowsInSection这个协议只在 tableview添加到?视图的时候??次,?且table上的数据 都是由数组提供,因此,需要先将数组中的元素删除,然后 让table的协议重新??遍进?重新赋值. 即:先修改数据源,再刷新table(使?relo

【UIKit】UITableView.07 编辑模式

[1]拖动好界面 [2]设置协议,数据源 [3]代码 1.声明可变数组,用来存放所有数据对象 @interface ViewController () @property(nonatomic,strong)NSMutableArray *mydata; @end 2.初始化数据[创建30个对象数据] - (void)viewDidLoad { [super viewDidLoad]; self.mydata=[NSMutableArray array]; for(int i =0;i<30;i+

iOS自定义全屏返回与tableView左划删除手势冲突解决

当自定义一个navigationController实现全屏右划返回时, 使用起来是不是很爽, 代码如下: - (void)viewDidLoad { [super viewDidLoad]; UIGestureRecognizer *gester = self.interactivePopGestureRecognizer; UIPanGestureRecognizer *panGesTer = [[UIPanGestureRecognizer alloc] initWithTarget:ge

iOS tableView右滑显示选择

如何使用UITableViewRowAction实现右滑选择呢? 1.在iOS8以前,我们实现tableview中滑动显示删除,置顶,更多等等的按钮时,都需要自己去实现,在iOS8中系统已经写好了,只要一个代理方法和一个类就行了 2.iOS8的协议对了一个方法,返回值是数组的tableview:editActionForRowAtIndexPath:方法,我们可以在方法内部写好几个按钮,然后放到数组中返回,那些按钮的类就是UITableviewRowAction 3.在UITableviewRo

TableView Editor and delete add Test

// // RootViewController.m // UI_tableView编辑 // // Created by Hunter on 15/12/5. // Copyright © 2015年 HaiTeng. All rights reserved. // #import "RootViewController.h" #import "SuperManModel.h" @interface RootViewController ()<UITable