UITableView editing and uitableview editing
The process of adding, deleting, moving, and basic operations to UITableView.
1. perform the following steps to initialize UITableView:
1> comply with the Protocol <UITableViewDelegate, UITableViewDataSource>
2> set proxy
3> the implementation method must implement two methods: (NSInteger) tableView: numberOfRowsInSection :( set the number of rows for each group)-(UITableViewCell *) tableView: cellForRowAtIndexPath: (set the display of each row)
2. add, delete, and move tableView (using the Protocol)
1> set whether to compile-(BOOL) tableView: canEditRowAtIndexPath: return YES to edit, return NO to edit
2> set the edit type-(UITableViewCellEditingStyle) tableView: editingStyleForRowAtIndexPath: return the UITableViewCellEditingStyleDelete delete operation, and UITableViewCellEditingStyleInsert add operation
3> complete the editing operation:
The add operation is similar to the delete operation. Implement-(void) tableView: commitEditingStyle: forRowAtIndexPath: During the operation, the data operation must be performed first, and then the Cell operation is performed.
Delete operation: (instance method) deleteSections: withRowAnimation: Delete Group deleteRowsAtIndexPaths: withRowAnimation: Delete row
Add operation: (instance method) insertRowsAtIndexPaths: withRowAnimation: Add row
Move operation:-(void) tableView: moveRowAtIndexPath: toIndexPath: during the operation, you must first operate the data and then operate the Cell.
Mobile Operation: data processing process: first store the data to be moved, delete the original data, and insert data
Call the instance method: moveRowAtIndexPath: toIndexPath: the original location of the two parameters.
Define the range of movement:-(NSIndexPath *) tableView: targetIndexPathForMoveFromRowAtIndexPath: (if it is limited to moving within the same group, determine whether the section of the input parameter is equal (the original position of the first parameter, the second parameter is the location to be moved), returns equal to the location to be moved, different return original location.
A simple implementation code written by an individual:
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; // indicates the operation type of the table. 14 BOOL addFlag; // status of the addButton button 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 // set proxy 31 self. rootView. tableView. delegate = self; 32 self. rootView. tableView. dataSource = self; 33 34 // initialization data 35 self. dataArray = [NSMutableArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "DataArray" ofType: @ "plist"]; 36 37 // Add deletion trigger button 38 self. navigationItem. RightBarButtonItem = self. editButtonItem; 39 self. editButtonItem. title = @ "delete"; 40 41 // Add data button 42 UIBarButtonItem * addButton = [[UIBarButtonItem alloc] initWithTitle: @ "add" style: UIBarButtonItemStylePlain target: self action: @ selector (insertAction :)]; 43 addFlag = NO; 44 self. navigationItem. leftBarButtonItem = addButton; 45 46 47} 48 49 # pragma mark --- implement proxy method --- 50 51 # pragma mark --- required Method --- 52 53 54/** 55 * number of rows per group of data 56 */57 58-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {59 return [[self. dataArray objectAtIndex: section] count]; 60} 61 62 63/** 64 * Set cell 65 */66 67-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {68 static NSString * cell_id = @ "cell_id"; 69 UITableViewCell * cel L = [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 --- non-essential proxy method --- 80 81/** 82 * Number of groups 83 */84 85-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {86 return [self. D AtaArray count]; 87} 88 89 90 # pragma mark --- edit TableView --- 91 92 93/** 94 * set whether 95 */96-(BOOL) can be edited) tableView :( UITableView *) tableView canEditRowAtIndexPath :( NSIndexPath *) indexPath {97 return YES; 98} 99 100 101/** 102 * set the editing type 103 */104-(UITableViewCellEditingStyle) tableView :( UITableView *) tableView editingStyleForRowAtIndexPath :( NSIndexPath *) indexPath {105 return style; 106} 107 108 109/** 110 * TableView operation 111 */112-(void) tableView :( UITableView *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyle forRowAtIndexPath :( NSIndexPath *) indexPath {113 // modify data, modify UI114 // modify data first, and then modify UI115 116 if (editingStyle = UITableViewCellEditingStyleDelete) {117 // Delete row 118 if ([self. dataArray objectAtIndex: indexPath. section] count] = 1) {119 // Delete Group 120 [self. dataArray removeO BjectAtIndex: indexPath. section]; 121 [tableView deleteSections: [NSIndexSet indexSetWithIndex: indexPath. section] withRowAnimation: UITableViewRowAnimationRight]; 122} else {123 124 // delete a single row 125 [[self. dataArray objectAtIndex: indexPath. section] removeObjectAtIndex: indexPath. row]; 126 [tableView deleteRowsAtIndexPaths: [NSArray arrayWithObjects: indexPath, nil] withRowAnimation: UITableViewRowAnimationRight]; 1 27} 128} else if (editingStyle = UITableViewCellEditingStyleInsert) {129 // Add data 130 // Add a row 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 Mobile --- 138 139 140/** 141 * Mobile row 142*143 */1 44-(void) tableView :( UITableView *) tableView moveRowAtIndexPath :( NSIndexPath *) sourceIndexPath toIndexPath :( NSIndexPath *) destinationIndexPath {145 146 // first store the data to be moved 147 NSString * str = [[self. dataArray objectAtIndex: sourceIndexPath. section] objectAtIndex: sourceIndexPath. row]; 148 // Delete the original data 149 [[self. dataArray objectAtIndex: sourceIndexPath. section] removeObjectAtIndex: sourceIndexPath. row]; 150 // Add to the place to be moved Add 151 data [[self. dataArray objectAtIndex: destinationIndexPath. section] insertObject: str atIndex: destinationIndexPath. row]; 152 [tableView moveRowAtIndexPath: sourceIndexPath toIndexPath: destinationIndexPath]; 153} 154 155 156/** 157 * limited moving range: 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 --- implementation of the Add button method --- 169 170 171/** 172 * click the delete button 173 */174-(void) setEditing :( BOOL) editing animated :( BOOL) animated {175 176 style = UITableViewCellEditingStyleDelete; 177 [super setEditin G: editing animated: animated]; 178 // associate tableView179 [self. rootView. tableView setEditing: editing animated: animated]; 180 self. editButtonItem. title = editing? @ "Done": @ "delete"; 181} 182 183 184/** 185 * click the Add button 186 */187-(void) insertAction :( UIBarButtonItem *) sender {188 style = UITableViewCellEditingStyleInsert; 189 addFlag =! AddFlag; 190 [self. rootView. tableView setEditing: addFlag animated: YES]; 191 sender. title = addFlag? @ "Done": @ "add"; 192} 193 194-(void) didReceiveMemoryWarning {195 [super didreceivemorywarning]; 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