對 UITableView 進行添加,刪除,移動,基本操作的流程.
1.初始化 UITableView 步驟:
1> 遵守協議 <UITableViewDelegate,UITableViewDataSource>
2> 設置代理
3> 實現方法 必須實現的方法有兩個 - (NSInteger)tableView:numberOfRowsInSection:(設置每個分組的行數)- (UITableViewCell*)tableView: cellForRowAtIndexPath: (設置每行的顯示)
2.對 tableView 進行增刪移動操作 (通過實現協議方法)
1>設置是否可編譯 - (BOOL)tableView: canEditRowAtIndexPath: 返回 YES 可以編輯,返回 NO 不可編輯
2>設置編輯的類型 - (UITableViewCellEditingStyle)tableView: editingStyleForRowAtIndexPath: 返回 UITableViewCellEditingStyleDelete 刪除操作,UITableViewCellEditingStyleInsert 添加操作
3>完成編輯操作:
添加和刪除操作相似,實現 - (void)tableView: commitEditingStyle: forRowAtIndexPath: 在進行操作時,一定是先對數據操作,然后操作 Cell
刪除操作:(實例方法)deleteSections: withRowAnimation: 刪除分組 deleteRowsAtIndexPaths: withRowAnimation: 刪除行
添加操作:(實例方法)insertRowsAtIndexPaths: withRowAnimation: 添加行
移動操作:- (void)tableView: moveRowAtIndexPath: toIndexPath:在進行操作時,一定是先對數據操作,然后操作 Cell
移動操作:數據處理過程:先存儲要移動數據,刪除原數據,插入數據
調用實例方法:moveRowAtIndexPath: toIndexPath: 兩個參數 原位置,要移動到的位置
限定移動的范圍:- (NSIndexPath*)tableView: targetIndexPathForMoveFromRowAtIndexPath: (如果限定在同一分組內移動,判斷傳入的參數的 section 是否相等,(第一個參數原位置,第二個參數要移動的位置),相等返回要移動到的位置,不同返回原位置.
個人寫的一個簡單的實現代碼:
1 // 2 // RootViewController.m 3 // Lesson10_HomeWork 4 // 5 // Created by Ager on 15/10/26. 6 // Copyright © 2015年 Ager. All rights reserved. 7 // 8 9 #import "RootViewController.h" 10 11 @interface RootViewController () 12 { 13 UITableViewCellEditingStyle style; //表示對 table 的操作類型 14 BOOL addFlag; //表示 addButton 按鈕的狀態 15 } 16 17 @end 18 19 @implementation RootViewController 20 21 - (void)loadView{ 22 self.rootView = [[RootView alloc]initWithFrame:[UIScreen mainScreen].bounds]; 23 self.view = self.rootView; 24 } 25 26 - (void)viewDidLoad { 27 [super viewDidLoad]; 28 // Do any additional setup after loading the view. 29 30 //設置代理 31 self.rootView.tableView.delegate = self; 32 self.rootView.tableView.dataSource = self; 33 34 //初始化數據 35 self.DataArray = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"DataArray" ofType:@"plist"]]; 36 37 //添加刪除觸發按鈕 38 self.navigationItem.rightBarButtonItem = self.editButtonItem; 39 self.editButtonItem.title = @"刪除"; 40 41 //添加 添加數據按鈕 42 UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(insertAction:)]; 43 addFlag = NO; 44 self.navigationItem.leftBarButtonItem = addButton; 45 46 47 } 48 49 #PRagma mark --- 實現代理方法 --- 50 51 #pragma mark --- 必須實現的方法 --- 52 53 54 /** 55 * 每組數據的行數 56 */ 57 58 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 59 return [[self.DataArray objectAtIndex:section] count]; 60 } 61 62 63 /** 64 * 設置cell 65 */ 66 67 - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 68 static NSString *cell_id = @"cell_id"; 69 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id]; 70 if (!cell) { 71 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell_id]; 72 } 73 74 cell.textLabel.text = [[self.DataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 75 return cell; 76 } 77 78 79 #pragma mark --- 不必須實現的代理方法 --- 80 81 /** 82 * 分組數 83 */ 84 85 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 86 return [self.DataArray count]; 87 } 88 89 90 #pragma mark --- 對 TableView 編輯 --- 91 92 93 /** 94 * 設置是否可以編輯 95 */ 96 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ 97 return YES; 98 } 99 100 101 /**102 * 設置編輯類型103 */104 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{105 return style;106 }107 108 109 /**110 * 完成 TableView 操作111 */112 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{113 //修改數據,修改UI114 //先修改數據,在修改UI115 116 if (editingStyle == UITableViewCellEditingStyleDelete) {117 //刪除行118 if ([[self.DataArray objectAtIndex:indexPath.section] count] == 1) {119 //刪除分組120 [self.DataArray removeObjectAtIndex:indexPath.section];121 [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationRight];122 }else {123 124 //刪除單行125 [[self.DataArray objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];126 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight];127 }128 }else if (editingStyle == UITableViewCellEditingStyleInsert){129 //添加數據130 //添加一行131 [[self.DataArray objectAtIndex:indexPath.section] insertObject:@"Ager" atIndex:indexPath.row];132 [tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight];133 }134 }135 136 137 #pragma mark --- cell 移動 ---138 139 140 /**141 * 移動行142 *143 */144 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{145 146 //先存儲要移動的數據147 NSString *str = [[self.DataArray objectAtIndex:sourceIndexPath.section] objectAtIndex:sourceIndexPath.row];148 //刪除原數據149 [[self.DataArray objectAtIndex:sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];150 //在要移動到地方添加數據151 [[self.DataArray objectAtIndex:destinationIndexPath.section] insertObject:str atIndex:destinationIndexPath.row];152 [tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];153 }154 155 156 /**157 * 限定移動范圍158 */159 - (NSIndexPath*)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{160 if (sourceIndexPath.section == proposedDestinationIndexPath.section) {161 return proposedDestinationIndexPath;162 }else {163 return sourceIndexPath;164 }165 }166 167 168 #pragma mark --- 添加按鈕的方法實現 ---169 170 171 /**172 * 點擊刪除按鈕173 */174 - (void)setEditing:(BOOL)editing animated:(BOOL)animated{175 176 style = UITableViewCellEditingStyleDelete;177 [super setEditing:editing animated:animated];178 //關聯 tableView179 [self.rootView.tableView setEditing:editing animated:animated];180 self.editButtonItem.title = editing ? @"完成":@"刪除";181 }182 183 184 /**185 * 點擊添加按鈕186 */187 - (void)insertAction:(UIBarButtonItem *)sender{188 style = UITableViewCellEditingStyleInsert;189 addFlag = !addFlag;190 [self.rootView.tableView setEditing:addFlag animated:YES];191 sender.title = addFlag ? @"完成":@"添加";192 }193 194 - (void)didReceiveMemoryWarning {195 [super didReceiveMemoryWarning];196 // Dispose of any resources that can be recreated.197 }198 199 /*200 #pragma mark - Navigation201 202 // In a storyboard-based application, you will often want to do a little preparation before navigation203 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {204 // Get the new view controller using [segue destinationViewController].205 // Pass the selected object to the new view controller.206 }207 */208 209 @end
新聞熱點
疑難解答