UITableView的编辑模式:
1.删除模式
_data = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
// Do any additional setup after loading the view.
_table = [[UITableView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
_table.backgroundColor = [UIColor grayColor];
_table.dataSource = self;
_table.delegate = self;
[self.view addSubview:_table];
//datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _data.count;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString* indentifier = @"cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
if (!cell) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier]autorelease];
}
cell.textLabel.text = [_data objectAtIndex:indexPath.row];
return cell;
}
//回调判断某行的cell是否能进入edit模式进行修改
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//如果有关cell的edit的回调都不实现,只实现该回调,那么cell会使用默认行为,但手指滑动cell时会自动出现删除按钮,点击删除按钮会回调该方法进行数据删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
[_data removeObjectAtIndex:indexPath.row];
//删除行并使用动画效果,并且该方法内部会自动调用reloaddata,刷新table的,不需要再使用reloaddata
/*
typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
UITableViewRowAnimationFade,
UITableViewRowAnimationRight, // slide in from right (or out to right)
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone, // available in iOS 3.0
UITableViewRowAnimationMiddle, // available in iOS 3.2. attempts to keep cell centered in the space it will/did occupy
UITableViewRowAnimationAutomatic = 100 // available in iOS 5.0. chooses an appropriate animation style for you
};
*/
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationTop];
//也可以使用reloaddata,不带动画效果的删除
//[tableView reloadData];
}
//delegate
//当table进入编辑模式时,回调该方法获取应该是哪种编辑模式:插入,删除,none(移动)。模式不实现系统是使用删除模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
/*
typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {
UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete,
UITableViewCellEditingStyleInsert
};
*/
return UITableViewCellEditingStyleDelete;
}
//当出现删除按钮时,回调该方法显示删除按钮的名字,默认不实现是delete
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0){
return @"删除";
}
这是没有使用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.移动模式
//回调判断某行的cell是否能进入edit模式进行修改
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
// Moving/reordering
// 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:
//回调判断某行的cell是否能移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//移动行的回调,只需设置数据源的数据顺序就可以了,不需要reloaddata
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSString* src = [_data objectAtIndex:sourceIndexPath.row];
[_data removeObjectAtIndex:sourceIndexPath.row];
[_data insertObject:src atIndex:destinationIndexPath.row];
}
//delegate
//当table进入编辑模式时,回调该方法获取应该是哪种编辑模式:插入,删除,none(移动)。模式不实现系统是使用删除模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
/*
typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {
UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete,
UITableViewCellEditingStyleInsert
};
*/
return UITableViewCellEditingStyleNone;
}
3.插入新的行
//回调判断某行的cell是否能进入edit模式进行修改
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//如果有关cell的edit的回调都不实现,只实现该回调,那么cell会使用默认行为,但手指滑动cell时会自动出现删除按钮,点击删除按钮会回调该方法进行数据删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
// [_data removeObjectAtIndex:indexPath.row];
// //删除行并使用动画效果
// /*
// typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
// UITableViewRowAnimationFade,
// UITableViewRowAnimationRight, // slide in from right (or out to right)
// UITableViewRowAnimationLeft,
// UITableViewRowAnimationTop,
// UITableViewRowAnimationBottom,
// UITableViewRowAnimationNone, // available in iOS 3.0
// UITableViewRowAnimationMiddle, // available in iOS 3.2. attempts to keep cell centered in the space it will/did occupy
// UITableViewRowAnimationAutomatic = 100 // available in iOS 5.0. chooses an appropriate animation style for you
// };
// */
// [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationTop];
//也可以使用reloaddata,不带动画效果的删除
//[tableView reloadData];
if (editingStyle==UITableViewCellEditingStyleInsert) {
[_data insertObject:@"6" atIndex:indexPath.row];
//在指定位置插入行并带上动画效果,该方法不但有动画效果并且内部会自动调用reloaddata刷新table,不需要再调用reloaddata来刷新
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}
}
//当table进入编辑模式时,回调该方法获取应该是哪种编辑模式:插入,删除,none(移动)。模式不实现系统是使用删除模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
/*
typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {
UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete,
UITableViewCellEditingStyleInsert
};
*/
return UITableViewCellEditingStyleInsert;
}
4.tableviewcell菜单选择栏
//回调设置长按cell时会否弹出菜单栏
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0){
return YES;
}
//回调设置哪些菜单选项能显示给用户使用
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0){
/*
– copy:
– cut:
– delete:
– paste:
Handling Selection Commands
– select:
– selectAll:
Handling Styled Text Editing
– toggleBoldface:
– toggleItalics:
– toggleUnderline:
Handling Writing Direction Changes
– makeTextWritingDirectionLeftToRight:
– makeTextWritingDirectionRightToLeft:
*/
return YES;
}
//回调当点击了菜单栏选项后的事件,根据不同SEL进行动作的实现
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0){
if(action==@selector(copy:)){
[UIPasteboard generalPasteboard].string = [_value objectAtIndex:indexPath.row];
}
}