码迷,mamicode.com
首页 > 其他好文 > 详细

tableView的编辑

时间:2016-06-02 18:13:00      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

首先记住声明编辑样式的属性  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;
    }
}

 

tableView的编辑

标签:

原文地址:http://www.cnblogs.com/menglingxu/p/5553714.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!