iOS编程-------UITableViewEdit_move -----> UITableView 编辑

//
//  AppDelegate.h
//  UI10_UITableViewEdit_move
//
//  Created by l on 15/9/14.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end





//
//  AppDelegate.m
//  UI10_UITableViewEdit_move
//
//  Created by l on 15/9/14.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate
- (void)dealloc{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    //rootVC
    RootViewController *rootVC = [[RootViewController alloc] init];

    rootVC.title = @"标题";
//    或
//    rootVC.navigationItem.title = @"标题";

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootVC];

    self.window.rootViewController = navigationController;

    [navigationController release];
    [rootVC release];

    //1.让tableView处于编辑状态
    //2.指定tableView哪些行可以被编辑
    //3.指定tableView编辑的样式(添加,删除)
    //4.提交编辑



    return YES;
}






//






//
//  RootViewController.h
//  UI10_UITableViewEdit_move
//
//  Created by l on 15/9/14.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end






//
//  RootViewController.m
//  UI10_UITableViewEdit_move
//
//  Created by l on 15/9/14.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()<UITableViewDelegate, UITableViewDataSource>


//联系人字典
@property (nonatomic, retain) NSMutableDictionary *studentDictionary;

//联系人有序分组名
@property (nonatomic, retain) NSMutableArray *keyArray;

@end

@implementation RootViewController

- (void)dealloc{
    [_studentDictionary release];
    [_keyArray release];
    [super dealloc];
}

#pragma mark-- tableViewDataSource
//分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return [_keyArray count];


}

//section对应的分组人数
- (NSUInteger)numberOfGroupInSection:(NSInteger)section{

    return [_studentDictionary[_keyArray[section]] count];

}

//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [self numberOfGroupInSection:section];
}

//区头
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    return _keyArray[section];
}


//右侧索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    return _keyArray;
}


//加载单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //1.重用标识符
    static NSString *identifier = @"cell";

    //2.重用队列中取可重用单元格
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    //3.判断,没有则创建新的单元格
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease];
    }

    //4.设置cell

    //1.取到对应的联系人
    NSDictionary *student = _studentDictionary[_keyArray[indexPath.section]][indexPath.row];

    //name
    NSString *name = [student objectForKey:@"name"];

    cell.textLabel.text = name;

    //phoneNumber
    NSString *phoneNumber = [student objectForKey:@"phoneNumber"];

    cell.detailTextLabel.text = phoneNumber;

//    //picture
//    NSString *picture = [student objectForKey:@"picture"];
//    UIImage *image = [UIImage imageNamed:picture];
//    cell.imageView.image = image;


//    cell.textLabel.text = [NSString stringWithFormat:@"%ld %ld", indexPath.section, indexPath.row];

    //5.返回cell
    return cell;

}

#pragma mark-- 编辑tableView
/**
 *  编辑tableView
 1.tableView 处于可编辑状态
 2.指定哪些行可以被编辑
 3.指定行的编辑样式
 4.提交编辑
 *
 *  @return
 */

     //1.让表视图处于可编辑状态
//(UIViewController的方法),作用是它里面的控件处于可编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{

    //(1).注意:重写该方法后,需要调用父类传递过来的该方法
    [super setEditing:editing animated:animated];

    //(2).让tableView可编辑
    [(UITableView *)self.view setEditing:editing animated:animated];
}

/***********************************移动*********/
//***移动tableView
//2.指定哪些行可以被移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;
}

//3.移动完成,源路径移动到目标路径
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

    //1.取到要移动的数据
    NSDictionary *studentInfo = _studentDictionary[_keyArray[sourceIndexPath.section]][sourceIndexPath.row];

    //数据引用计数+1,防止从源数组中移除变成野指针
    [studentInfo retain];


    //2.从源地址移除数据
    [_studentDictionary[_keyArray[sourceIndexPath.section]] removeObject:studentInfo];

    //3.数据添加到目标地址
    [_studentDictionary[_keyArray[destinationIndexPath.section]] insertObject:studentInfo atIndex:destinationIndexPath.row];

    //数据引用计数-1
    [studentInfo release];

    //操作UI
    [tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];


}

//4.限制跨区移动
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{

    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {

        return proposedDestinationIndexPath;

    }else{

        return sourceIndexPath;
    }
}

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

    //2.指定哪些行可以被编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
//
//    if (indexPath.section == 0) {
//        return YES;
//    }else{
//        return NO;
//    }

    return YES;//默认的

}

    //3.指定行的编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    //指定插入还是删除,默认为删除
    if (indexPath.section == 0) {
        return UITableViewCellEditingStyleInsert;
    }else{
        return UITableViewCellEditingStyleDelete;
    }
}

   //4.提交编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    //tableView 开始更新
    [tableView beginUpdates];

    //所有编辑相关操作,写在开始和结束之间

    //1.判断编辑状态
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        //联系人分组
        NSMutableArray *studentArray = _studentDictionary[_keyArray[indexPath.section]];

        //如果想要没有联系人分组,需要添加判断
        if (studentArray.count == 1) {


            //如果当前分组仅有一个联系人,删除后数组为空,删除当前分组

            //1.操作数据源,删除该分组
            //分组名
            NSString *groupName = _keyArray[indexPath.section];

            //从字典中删除该分组
            [_studentDictionary removeObjectForKey:groupName];

            //从keyArray中删除该组名
            [_keyArray removeObject:groupName];

            //2.操作UI 删除分区
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:(UITableViewRowAnimationTop)];


        }else{


            //2.删除操作,先操作数据源后修改UI

            //操作数据源
            //(1)获取当前的联系人
            NSDictionary *studentInfo = studentArray[indexPath.row];

            //        NSDictionary *studentInfo = _studentDictionary[_keyArray[indexPath.section]][indexPath.row];

            //(2)从数组中删除联系人
            [studentArray removeObject:studentInfo];

            //修改UI
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];

        }


    }else{

        //操作数据
        //要插入的数据
        NSDictionary *newStudent = @{@"name" : @"bilityniu", @"phoneNumber" : @"123456"};

        //获取要插入的数组
        NSMutableArray *studentArray = _studentDictionary[@"B"];

        //把数据插入到数组中
        [studentArray addObject:newStudent];

        //操作UI
        //创建插入下标路径
        //获取分区下标
        NSUInteger indexPathForSection = [_keyArray indexOfObject:@"B"];

        //获取联系人的分组下标
        NSUInteger indexPathForRow = [studentArray indexOfObject:newStudent];

        //目标indexPath
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:indexPathForRow inSection:indexPathForSection];

        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];

    }

    //结束更新
    [tableView endUpdates];

}

#pragma mark-- 重写loadView
- (void)loadView{


    //创建tableView,并设置为Controller的view
    UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];

    tableView.rowHeight = 110;

    tableView.delegate = self;

    tableView.dataSource = self;

    self.view = tableView;

    [tableView release];

}

#pragma mark-- viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];

    //添加可编辑按钮
    //每一个controller, 都自带一个editButtonItem (系统提供的)
    //作用是 编辑 和 编辑完成
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

//     [(UITableView *)self.view setEditing:YES animated:nil];

    //解析plist文件,根据根节点解析
    //1.获取文件路径
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"Class25ContactList" ofType:@"plist"];

    //2.解析plist文件,根据根节点解析
    NSMutableDictionary *contactDic = [NSMutableDictionary dictionaryWithContentsOfFile:filepath];

//    _studentDictionary //

    //给联系人赋值
    self.studentDictionary = contactDic;
    NSLog(@"%@", _studentDictionary);

    //给联系人分组赋值
    _keyArray = [[[_studentDictionary allKeys] sortedArrayUsingSelector:@selector(compare:)] mutableCopy];//把不可变数组转变为可变数组
    NSLog(@"%@", _keyArray);



    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值