有两种方式可以实现,一种是系统自带的方法,一种是在tableview上添加长按手势来实现。
第一种方法比较简单:
实现tableview的UITableViewDataSource中的几个方法即可。
这样实现的效果,是只能拖动右侧的系统拖动按钮才能进行拖动。
1、先设置 self.tableView.editing = YES;
或者通过一个编辑按钮来控制是开启还是关闭编辑状态(例如,购物车里点击全选就进入了编辑状态一样)
这是一切开始的首要步骤
2、实现代理方法代码如下
#pragma mark-- 系统实现拖动cell
//移动行,是否可编辑
- (BOOL)tableView:(UITableView*)tableViewcanEditRowAtIndexPath:(NSIndexPath*)indexPath{
return YES;
}
//编辑模式
- (void)setEditing:(BOOL)editinganimated:(BOOL)animated{
[self.tableView setEditing:YES animated:YES];
}
#pragma mark-- 左滑编辑删除(开启编辑状态下,点击删除按钮删除,关闭编辑状态下,实现该方法,左滑进行删除)
- (void)tableView:(UITableView*)tableViewcommitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath*)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//删除操作
NSString*letter = [NSStringstringWithFormat:@"%@",self.letterArr[indexPath.row]];
[self.letterArrremoveObject:letter];
}
[self.tableView reloadData];
}
//编辑样式(前面的减号图标隐藏,但位置还是空出来了)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
//允许重新排序单元格
-(BOOL)tableView:(UITableView*)tableViewcanMoveRowAtIndexPath:(NSIndexPath*)indexPath{
return YES;
}
//执行重新排序的操作
- (void)tableView:(UITableView*)tableViewmoveRowAtIndexPath:(NSIndexPath*)sourceIndexPathtoIndexPath:(NSIndexPath*)destinationIndexPath{
// 取出要拖动的模型数据
NSString*letter = [NSStringstringWithFormat:@"%@",self.letterArr[sourceIndexPath.row]];
//删除之前行的数据
[self.letterArr removeObject:letter];
// 插入数据到新的位置
[self.letterArr insertObject:letterat Index:destinationIndexPath.row];
}
第二种方法:长按手势
1、给tableview添加长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressRecognizer:)];
[self.tableView addGestureRecognizer:longPress];
2、手势实现方法
//cell长按拖动排序
- (void)longPressRecognizer:(UILongPressGestureRecognizer *)longPress{
//获取长按的点及cell
CGPoint location = [longPress locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
UIGestureRecognizerState state = longPress.state;
staticUIView *snapView =nil;
staticNSIndexPath *sourceIndex =nil;
switch(state) {
caseUIGestureRecognizerStateBegan:{
if(indexPath) {
sourceIndex = indexPath;
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
snapView = [selfcustomViewWithTargetView:cell];
__blockCGPoint center = cell.center;
snapView.center = center;
snapView.alpha =0.0;
[self.tableView addSubview:snapView];
[UIView animateWithDuration:0.1animations:^{
center.y = location.y;
snapView.center = center;
snapView.transform = CGAffineTransformMakeScale(1.05,1.05);
snapView.alpha =0.5;
cell.alpha =0.0;
}];
}
}
break;
caseUIGestureRecognizerStateChanged:{
CGPoint center = snapView.center;
center.y = location.y;
snapView.center = center;
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:sourceIndex];
cell.alpha =0.0;
if(indexPath && ![indexPath isEqual:sourceIndex]) {
[self.letterArr exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndex.row];
[self.tableView moveRowAtIndexPath:sourceIndex toIndexPath:indexPath];
sourceIndex = indexPath;
}
}
break;
default:{
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:sourceIndex];
[UIView animateWithDuration:0.25animations:^{
snapView.center = cell.center;
snapView.transform = CGAffineTransformIdentity;
snapView.alpha =0.0;
cell.alpha =1.0;
} completion:^(BOOLfinished) {
[snapView removeFromSuperview];
snapView =nil;
}];
sourceIndex =nil;
}
break;
}
}
//截取选中cell
- (UIView *)customViewWithTargetView:(UIView *)target{
UIGraphicsBeginImageContextWithOptions(target.bounds.size,NO,0);
[target.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIView *snapshot = [[UIImageView alloc] initWithImage:image];
snapshot.layer.masksToBounds =NO;
snapshot.layer.cornerRadius =0.0;
snapshot.layer.shadowOffset = CGSizeMake(-5.0,0.0);
snapshot.layer.shadowRadius =5.0;
snapshot.layer.shadowOpacity =0.4;
returnsnapshot;
}