TableView Edit

//

// RootViewController.m


 

#import "RootViewController.h"

 

@interfaceRootViewController()<UITableViewDelegate,UITableViewDataSource>

 

@property (nonatomic,strong)UITableView*tableView;

 

@property (nonatomic,strong)NSMutableArray*mArr;

@property(nonatomic,strong)NSMutableDictionary *mDic;

@property (nonatomic,strong)NSMutableDictionary*imageDic;

@property (nonatomic,strong)NSMutableArray*imageArr;

@end

 

@implementationRootViewController

 

- (void)viewDidLoad {

   [superviewDidLoad];

 

self.title = @"suprise";

   // 创建tableView

self.tableView = [[UITableViewalloc]initWithFrame:[UIScreenmainScreen].boundsstyle:UITableViewStylePlain];

   // 设置分割线颜色

self.tableView.separatorColor =[UIColorredColor];

   //设置分割线格式

self.tableView.separatorStyle =UITableViewCellSeparatorStyleSingleLine;

   //设置代理

self.tableView.dataSource = self;

self.tableView.delegate = self;

   //添加到试图上

   [self.viewaddSubview:self.tableView];

   // 给导航栏右边加一分编辑按钮,(编辑按钮是UIViewController自带的)

self.navigationItem.rightBarButtonItem =self.editButtonItem;

self.tableView.backgroundColor = [UIColorredColor];

   // 获取数据

   [selfgetDate];

 

self.tableView.rowHeight = 150;

 

 

}

     //  一、编辑的四步

  //1、进入编辑状态

// 2、确定哪些cell可以被编辑

// 3、确定编辑模式

// 4、完成编辑

 

// 1、点击右按钮触发的编辑方法(既可以添加删除,又可以移动)

- (void)setEditing:(BOOL)editinganimated:(BOOL)animated{

   [supersetEditing:editinganimated:animated];

   // 将tableView置为编辑模式下。

   [self.tableViewsetEditing:editinganimated:animated];

}

 

// 2、设置可以被编辑的cell

- (BOOL)tableView:(UITableView*)tableViewcanEditRowAtIndexPath:(NSIndexPath *)indexPath{

 

return YES;

}

 

// 3、设置编辑模式

-(UITableViewCellEditingStyle)tableView:(UITableView*)tableVieweditingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

if (indexPath.section == 0) {

returnUITableViewCellEditingStyleInsert;

    }

returnUITableViewCellEditingStyleDelete;

}

 

  //4、完成编辑

- (void)tableView:(UITableView*)tableViewcommitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath*)indexPath{

     // 先操作数据,再操作UI(控件)

if (editingStyle ==UITableViewCellEditingStyleDelete){

       [self.mDic[self.mArr[indexPath.section]] removeObjectAtIndex:

indexPath.row];

       [self.imageDic[self.imageArr[indexPath.section]] removeObjectAtIndex:

indexPath.row];

if([self.mDic[self.mArr[indexPath.section]] count] == 0) {

           // 删除键值对

           [self.mDicremoveObjectForKey:self.mArr[indexPath.section]];

           [self.imageDicremoveObjectForKey:self.imageArr[indexPath.section]];

//            //删除分组

           [self.mArrremoveObjectAtIndex:indexPath.section];

           [self.imageArrremoveObjectAtIndex:indexPath.section];

           // 删除分区(删除控件)

           [tableViewdeleteSections:[NSIndexSetindexSetWithIndex:indexPath.section]withRowAnimation:UITableViewRowAnimationLeft];

       }

    }

//   else{

//       // 先操作数据

//       NSString *key = self.mArr[indexPath.section];

//       NSMutableArray *arr = self.mDic[key];

//       [arrinsertObject:@"布鲁克" atIndex:indexPath.row];

//       

       NSString *key1 = self.imageArr[indexPath.section];

       NSMutableArray *arr1 = self.imageDic[key1];

       [arr1insertObject:[UIImageimageNamed:@"h2.jpeg"]atIndex:indexPath.row];

//

//        // 再操作UI

//       [tableViewinsertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationBottom];

//   }

       [self.tableViewreloadData];

}

 

 // 二、移动的三个步骤

 // 1、设置处于编辑状态

 // 2、设置哪些cell可以被编辑

 //3、编辑过程

 

//2、设置哪些cell可编辑(移动)

- (BOOL)tableView:(UITableView*)tableViewcanMoveRowAtIndexPath:(NSIndexPath *)indexPath{

if (indexPath.section == 0) {

return NO;

    }

return YES;

}

 

 // 3、移动过程

- (void)tableView:(UITableView*)tableViewmoveRowAtIndexPath:(NSIndexPath*)sourceIndexPathtoIndexPath:(NSIndexPath *)destinationIndexPath{

   //获取起始点数组

NSMutableArray *arrSource =self.mDic[self.mArr[sourceIndexPath.section]];

NSMutableArray *imageArrSource =self.imageDic[self.imageArr[sourceIndexPath.section]];

   // 获取终点数组

NSMutableArray *arrDesti =self.mDic[self.mArr[destinationIndexPath.section]];

NSMutableArray *imageArrDesti =self.imageDic[self.mArr[destinationIndexPath.section]];

 

   // 先将cell对应的数据用字符串存储

NSString *str =arrSource[sourceIndexPath.row];

NSString *strImage =imageArrSource[sourceIndexPath.row];

 

   // 删除数据

   [arrSourceremoveObject:arrSource[sourceIndexPath.row]];

   [imageArrSourceremoveObject:imageArrSource[sourceIndexPath.row]];

   //插入数据

   [arrDestiinsertObject:stratIndex:destinationIndexPath.row];

   [imageArrDestiinsertObject:strImageatIndex:destinationIndexPath.row];

 

if ([arrSource count] == 0) {

       // 删除空数组

       [self.mDicremoveObjectForKey:self.mArr[sourceIndexPath.section]];

       [self.imageDicremoveObjectForKey:self.imageArr[sourceIndexPath.section]];

       // 删除头标题

       [self.mArrremoveObjectAtIndex:sourceIndexPath.section];

       [self.imageArrremoveObjectAtIndex:sourceIndexPath.section];

       // 删除分区

       [tableViewdeleteSections:[NSIndexSetindexSetWithIndex:sourceIndexPath.section]withRowAnimation:UITableViewRowAnimationFade];

    }

 

 

     // 重新加载数据,刷新视图

   [self.tableViewreloadData];

 

//   // 刷新某个分区

//   [tableViewreloadSections:[NSIndexSetindexSetWithIndex:sourceIndexPath.section]withRowAnimation:UITableViewRowAnimationFade];

}

 

    // 限制跨区移动

- (NSIndexPath *)tableView:(UITableView*)tableViewtargetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath*)sourceIndexPathtoProposedIndexPath:(NSIndexPath*)proposedDestinationIndexPath{

 

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

returnsourceIndexPath;

    }

returnproposedDestinationIndexPath;

}

 

 

        // 设置分区个数

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{

return [self.mArr count];

}

       // 设置分区行数

- (NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{

return [self.mDic[self.mArr[section]]count];

}

 

 

- (UITableViewCell *)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath{

 

UITableViewCell *cell =[tableViewdequeueReusableCellWithIdentifier:@"reuse"];

if (cell == nil) {

cell =[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:@"reuse"];

    }

 

UIImage *image =[UIImageimageNamed:self.imageDic[self.imageArr[indexPath.section]][indexPath.row]];

cell.imageView.image = image;

 

cell.textLabel.text =self.mDic[self.mArr[indexPath.section]][indexPath.row];

 

return cell;

}

         // 设置头标题

- (NSString *)tableView:(UITableView*)tableViewtitleForHeaderInSection:(NSInteger)section{

returnself.mArr[section];

}

 

 

 

- (void)getDate{

   // 一个字典含有三个数组(对应三个分区)

   // 将key值作为分区头标题(字典与数组可变)

 

NSMutableArray *mArr1 =[NSMutableArrayarrayWithObjects:@"路飞",@"索隆",@"香吉士",nil];

NSMutableArray *mArr2 =[NSMutableArrayarrayWithObjects:@"乌索普", @"乔巴",@"弗兰奇",nil];

NSMutableArray *mArr3 =[NSMutableArrayarrayWithObjects:@"娜美",@"罗宾",nil];

self.mDic = [NSMutableDictionarydictionaryWithObjectsAndKeys:mArr1,@"A",mArr2,@"B",mArr3,@"C",nil];

self.mArr =[NSMutableArrayarrayWithObjects:@"A",@"B",@"C",nil];

 

NSMutableArray *imageArr1 =[NSMutableArrayarrayWithObjects:@"h4.jpeg",@"h2.jpeg",@"h6.jpeg",nil];

NSMutableArray *imageArr2 =[NSMutableArrayarrayWithObjects:@"h3.jpeg",@"h7.jpeg",@"h8.jpeg",nil];

NSMutableArray *imageArr3 =[NSMutableArrayarrayWithObjects:@"h5.jpeg",@"h1.jpeg",nil];

self.imageDic = [NSMutableDictionarydictionaryWithObjectsAndKeys:imageArr1,@"A",imageArr2,@"B",imageArr3,@"C",nil];

self.imageArr =[NSMutableArrayarrayWithObjects:@"A",@"B",@"C",nil];

 

}

 

- (UIView *)tableView:(UITableView*)tableViewviewForHeaderInSection:(NSInteger)section{

 

UILabel *label =[[UILabelalloc]initWithFrame:CGRectMake(130,0,40,30)];

label.text = self.mArr[section];

label.backgroundColor = [UIColorredColor];

UIView *view = [[UIViewalloc]init];

view.backgroundColor =[UIColoryellowColor];

   [viewaddSubview:label];

 

return view;

}

 

 

@end

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值