TableView Editor and delete add Test

 

哈哈哈哈😆 ------------- 大哥原創數組

 
 

請多多指教 .....................................................atom



//
// 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 ()<UITableViewDelegate,UITableViewDataSource> @property(nonatomic,strong)UITableView *tableView; @property(nonatomic,strong)NSMutableDictionary *dataDic; //數據,字典 @property(nonatomic,strong)NSMutableArray *allKeys; //全部key @property(nonatomic,strong)NSMutableDictionary *dic; //接收數據,字典 @end @implementation RootViewController NSString *cell_id = @"cell_id"; #pragma mark 處理數據 - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ([super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { _dic = [NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BJS151043" ofType:@"plist"]]; self.allKeys = [_dic allKeys].mutableCopy; [self.allKeys sortUsingSelector:@selector(compare:)]; _dataDic = [NSMutableDictionary dictionary]; for (NSString *key in self.allKeys) { NSMutableArray *array = [_dic objectForKey:key]; NSMutableArray *arra = [NSMutableArray array]; for (NSDictionary *di in array) { SuperManModel *model = [[SuperManModel alloc] init]; [model setValuesForKeysWithDictionary:di]; [arra addObject:model]; } [_dataDic setObject:arra forKey:key]; } NSLog(@"%@",_dataDic); } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:0]; self.tableView.rowHeight = 60; self.tableView.delegate = self; self.tableView.dataSource = self; [self.view addSubview:self.tableView]; // 註冊cell [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cell_id]; //右按鈕 UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"編輯" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)]; self.navigationItem.rightBarButtonItem = right; //系統提供的編輯按鈕 // self.navigationItem.rightBarButtonItem = self.editButtonItem; // self.editButtonItem.title = @"編輯"; } #pragma mark 代理方法 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { //經過key值,找Value值 return [self.dataDic[_allKeys[section]] count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id]; SuperManModel *model = self.dataDic[_allKeys [indexPath.section]][indexPath.row]; cell.textLabel.text = model.name; cell.imageView.image = [UIImage imageNamed:model.picture]; return cell; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.allKeys.count; } - (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return _allKeys; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return _allKeys[section]; } #pragma mark TableView編輯 #pragma rightAction事件 //第一步:是頁面處於可編輯狀態 - (void)rightAction:(UIBarButtonItem *)sender { //設置當前頁面能夠被編輯 //當點擊的時候,頁面應該處於可編輯狀態,而且按鈕文字變成"完成" if ([sender.title isEqualToString:@"編輯"]) { sender.title = @"完成"; [_tableView setEditing:YES animated:YES]; //讓按鈕處於可編輯狀態 }else{ //當點擊完成的時候,應該讓頁面處於不可編輯狀態,而且按鈕文字顯示爲"編輯" sender.title = @"編輯"; [_tableView setEditing:NO animated:YES]; } } //第二步:指定哪些行能夠被編輯 -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 2) { return NO; } //(這裏我讓第三組,不可編輯作下演示) return YES; } //第三步:根據路徑指定tableView的編輯樣式 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { return UITableViewCellEditingStyleInsert; } //這裏我設置第一行爲增長,其餘的爲刪除(作下演示) return UITableViewCellEditingStyleDelete; } //第四步:根據編輯風格,完成編輯(先修改數據源,再修改UI) - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { //判斷 if (editingStyle == UITableViewCellEditingStyleDelete) { //刪除 NSMutableArray *array = _dataDic[_allKeys[indexPath.section]];//找到相應數組 if (array.count == 1) { //刪除數據 // [_dataDic removeObjectForKey:_allKeys[indexPath.section]]; [_allKeys removeObjectAtIndex:indexPath.section]; //刪除UI [_tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationTop]; }else{ //刪除數據 [array removeObjectAtIndex:indexPath.row]; //刪除UI [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop]; } }else{ //增長 NSDictionary *diccc = [NSDictionary dictionaryWithObjectsAndKeys:@"熊大",@"name",@"",@"gender",@"",@"hobby",@"11111",@"phoneNumber",@22,@"age",@"1.png",@"picture", nil]; SuperManModel *sm = [[SuperManModel alloc] init]; [sm setValuesForKeysWithDictionary:diccc]; // 找分組 NSMutableArray *array = _dataDic[_allKeys[indexPath.section]]; //添加元素 [array insertObject:sm atIndex:indexPath.row + 1];//(添加到這一行下面去) //更新UI NSIndexPath *newIndex = [NSIndexPath indexPathForRow:indexPath.row +1 inSection:indexPath.section]; [_tableView insertRowsAtIndexPaths:@[newIndex] withRowAnimation:UITableViewRowAnimationLeft]; } } #pragma tableView移動 /* //第一步:是頁面處於,可編輯狀態 - (void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated:animated]; [self.tableView setEditing:editing animated:animated]; //編輯按鈕文字 self.navigationItem.rightBarButtonItem.title = editing ? @"完成" : @"編輯"; } //第二步:指定tableView哪些行能夠移動 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } 動數據 NSMutable //第三步:移動完成 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { //先獲取移Array *array = _dataDic[_allKeys[sourceIndexPath.section]]; //獲取移動數據中的對象 NSDictionary *dic = [array objectAtIndex:sourceIndexPath.row]; //先刪除,再添加 //刪除 [array removeObjectAtIndex:sourceIndexPath.row]; //添加 [array insertObject:dic atIndex:destinationIndexPath.row]; } //第四步:tableView 檢測跨區移動 - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath { if (proposedDestinationIndexPath.section == sourceIndexPath.section) { return proposedDestinationIndexPath; } return sourceIndexPath; } */ /* #pragma Mark cell 編輯自定義操做 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { //這個方法,寫這不用管就OK } - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { NSLog(@"刪除"); NSMutableArray *array = _dataDic[_allKeys[indexPath.section]]; if (array.count == 1) { [_dataDic removeObjectForKey:_allKeys[indexPath.section]]; [_allKeys removeObjectAtIndex:indexPath.section]; [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationTop]; }else{ [array removeObjectAtIndex:indexPath.row]; [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop]; } }]; UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"增長" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { NSLog(@"增長"); NSDictionary *diccc = [NSDictionary dictionaryWithObjectsAndKeys:@"熊大",@"name",@"男",@"gender",@"玩",@"hobby",@"11111",@"phoneNumber",@22,@"age",@"1.png",@"picture", nil]; SuperManModel *mode = [[SuperManModel alloc] init]; [mode setValuesForKeysWithDictionary:diccc]; NSMutableArray *array = _dataDic[_allKeys[indexPath.section]]; [array insertObject:mode atIndex:indexPath.row]; [self. tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; }]; return @[action1,action2]; } */ @end

相關文章
相關標籤/搜索