UITableView编辑模式详解
UITableView的编辑模式:
1.删除模式
1 _data = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil]; 2 // Do any additional setup after loading the view. 3 _table = [[UITableView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)]; 4 _table.backgroundColor = [UIColor grayColor]; 5 _table.dataSource = self; 6 _table.delegate = self; 7 [self.view addSubview:_table];
1 //datasource 2 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 3 return _data.count; 4 } 5 6 // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier: 7 // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls) 8 9 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 10 static NSString* indentifier = @"cell"; 11 UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:indentifier]; 12 if (!cell) { 13 cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier]autorelease]; 14 } 15 cell.textLabel.text = [_data objectAtIndex:indexPath.row]; 16 return cell; 17 } 18 //回调判断某行的cell是否能进入edit模式进行修改 19 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ 20 return YES; 21 } 22 23 //如果有关cell的edit的回调都不实现,只实现该回调,那么cell会使用默认行为,但手指滑动cell时会自动出现删除按钮,点击删除按钮会回调该方法进行数据删除 24 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ 25 26 [_data removeObjectAtIndex:indexPath.row]; 27 //删除行并使用动画效果,并且该方法内部会自动调用reloaddata,刷新table的,不需要再使用reloaddata 28 /* 29 typedef NS_ENUM(NSInteger, UITableViewRowAnimation) { 30 UITableViewRowAnimationFade, 31 UITableViewRowAnimationRight, // slide in from right (or out to right) 32 UITableViewRowAnimationLeft, 33 UITableViewRowAnimationTop, 34 UITableViewRowAnimationBottom, 35 UITableViewRowAnimationNone, // available in iOS 3.0 36 UITableViewRowAnimationMiddle, // available in iOS 3.2. attempts to keep cell centered in the space it will/did occupy 37 UITableViewRowAnimationAutomatic = 100 // available in iOS 5.0. chooses an appropriate animation style for you 38 }; 39 */ 40 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationTop]; 41 //也可以使用reloaddata,不带动画效果的删除 42 //[tableView reloadData]; 43 } 44 45 //delegate 46 //当table进入编辑模式时,回调该方法获取应该是哪种编辑模式:插入,删除,none(移动)。模式不实现系统是使用删除模式 47 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ 48 49 /* 50 typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) { 51 UITableViewCellEditingStyleNone, 52 UITableViewCellEditingStyleDelete, 53 UITableViewCellEditingStyleInsert 54 }; 55 */ 56 return UITableViewCellEditingStyleDelete; 57 } 58 //当出现删除按钮时,回调该方法显示删除按钮的名字,默认不实现是delete 59 - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0){ 60 return @"删除"; 61 }
这是没有使用table进入编辑模式下的删除效果
当[_table setEditing:YES];设置为进入编辑模式下的删除效果
如果在出现删除按钮把原本cell的subview遮挡了,可以在以下回调中使用如下方法来调整subview的位置
//点击删除按钮后的回调
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
UIButton* b = (UIButton*)[cell viewWithTag:1];
[UIView beginAnimations:@"" context:nil];
[UIView animateWithDuration:0.5 animations:^{
b.frame = CGRectMake(b.frame.origin.x-15, b.frame.origin.y, b.frame.size.width, b.frame.size.height);
}];
[UIView commitAnimations];
}
//将要出现删除按钮时的回调,调整subview的位置
-(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
UIButton* b = (UIButton*)[cell viewWithTag:1];
[UIView beginAnimations:@"" context:nil];
[UIView animateWithDuration:0.5 animations:^{
b.frame = CGRectMake(b.frame.origin.x-15, b.frame.origin.y, b.frame.size.width, b.frame.size.height);
}];
[UIView commitAnimations];
}
//删除按钮消失后的回调,用于重新调整subview到原来位置
-(void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
UIButton* b = (UIButton*)[cell viewWithTag:1];
[UIView beginAnimations:@"" context:nil];
[UIView animateWithDuration:0.5 animations:^{
b.frame = CGRectMake(b.frame.origin.x+15, b.frame.origin.y, b.frame.size.width, b.frame.size.height);
}];
[UIView commitAnimations];
}
2.移动模式
1 //回调判断某行的cell是否能进入edit模式进行修改 2 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ 3 return YES; 4 } 5 6 // Moving/reordering 7 8 // Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath: 9 //回调判断某行的cell是否能移动 10 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ 11 return YES; 12 } 13 14 //移动行的回调,只需设置数据源的数据顺序就可以了,不需要reloaddata 15 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ 16 17 NSString* src = [_data objectAtIndex:sourceIndexPath.row]; 18 [_data removeObjectAtIndex:sourceIndexPath.row]; 19 [_data insertObject:src atIndex:destinationIndexPath.row]; 20 } 21 22 //delegate 23 //当table进入编辑模式时,回调该方法获取应该是哪种编辑模式:插入,删除,none(移动)。模式不实现系统是使用删除模式 24 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ 25 26 /* 27 typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) { 28 UITableViewCellEditingStyleNone, 29 UITableViewCellEditingStyleDelete, 30 UITableViewCellEditingStyleInsert 31 }; 32 */ 33 return UITableViewCellEditingStyleNone; 34 }
3.插入新的行
1 //回调判断某行的cell是否能进入edit模式进行修改 2 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ 3 return YES; 4 } 5 6 //如果有关cell的edit的回调都不实现,只实现该回调,那么cell会使用默认行为,但手指滑动cell时会自动出现删除按钮,点击删除按钮会回调该方法进行数据删除 7 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ 8 9 // [_data removeObjectAtIndex:indexPath.row]; 10 // //删除行并使用动画效果 11 // /* 12 // typedef NS_ENUM(NSInteger, UITableViewRowAnimation) { 13 // UITableViewRowAnimationFade, 14 // UITableViewRowAnimationRight, // slide in from right (or out to right) 15 // UITableViewRowAnimationLeft, 16 // UITableViewRowAnimationTop, 17 // UITableViewRowAnimationBottom, 18 // UITableViewRowAnimationNone, // available in iOS 3.0 19 // UITableViewRowAnimationMiddle, // available in iOS 3.2. attempts to keep cell centered in the space it will/did occupy 20 // UITableViewRowAnimationAutomatic = 100 // available in iOS 5.0. chooses an appropriate animation style for you 21 // }; 22 // */ 23 // [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationTop]; 24 //也可以使用reloaddata,不带动画效果的删除 25 //[tableView reloadData]; 26 27 if (editingStyle==UITableViewCellEditingStyleInsert) { 28 [_data insertObject:@"6" atIndex:indexPath.row]; 29 30 //在指定位置插入行并带上动画效果,该方法不但有动画效果并且内部会自动调用reloaddata刷新table,不需要再调用reloaddata来刷新 31 32 [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle]; 33 } 34 35 } 36 37 //当table进入编辑模式时,回调该方法获取应该是哪种编辑模式:插入,删除,none(移动)。模式不实现系统是使用删除模式 38 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ 39 40 /* 41 typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) { 42 UITableViewCellEditingStyleNone, 43 UITableViewCellEditingStyleDelete, 44 UITableViewCellEditingStyleInsert 45 }; 46 */ 47 return UITableViewCellEditingStyleInsert; 48 }
4.tableviewcell菜单选择栏
1 //回调设置长按cell时会否弹出菜单栏 2 - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0){ 3 4 return YES; 5 } 6 //回调设置哪些菜单选项能显示给用户使用 7 - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0){ 8 /* 9 10 – copy: 11 – cut: 12 – delete: 13 – paste: 14 15 Handling Selection Commands 16 17 – select: 18 – selectAll: 19 20 Handling Styled Text Editing 21 22 – toggleBoldface: 23 – toggleItalics: 24 – toggleUnderline: 25 26 Handling Writing Direction Changes 27 28 – makeTextWritingDirectionLeftToRight: 29 – makeTextWritingDirectionRightToLeft: 30 */ 31 return YES; 32 } 33 //回调当点击了菜单栏选项后的事件,根据不同SEL进行动作的实现 34 - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0){ 35 36 if(action==@selector(copy:)){ 37 [UIPasteboard generalPasteboard].string = [_value objectAtIndex:indexPath.row]; 38 } 39 }