有兩種方式可以實現(xiàn),一種是系統(tǒng)自帶的方法,一種是在tableview上添加長按手勢來實現(xiàn)。
第一種方法比較簡單:
實現(xiàn)tableview的UITableViewDataSource中的幾個方法即可。

這樣實現(xiàn)的效果,是只能拖動右側(cè)的系統(tǒng)拖動按鈕才能進行拖動。
1、先設(shè)置 self.tableView.editing = YES;
或者通過一個編輯按鈕來控制是開啟還是關(guān)閉編輯狀態(tài)(例如,購物車里點擊全選就進入了編輯狀態(tài)一樣)
這是一切開始的首要步驟
2、實現(xiàn)代理方法代碼如下
#pragma mark-- 系統(tǒng)實現(xiàn)拖動cell
//移動行,是否可編輯
- (BOOL)tableView:(UITableView*)tableViewcanEditRowAtIndexPath:(NSIndexPath*)indexPath{
? ? return YES;
}
//編輯模式
- (void)setEditing:(BOOL)editinganimated:(BOOL)animated{
? ? [self.tableView setEditing:YES animated:YES];
}
#pragma mark-- 左滑編輯刪除(開啟編輯狀態(tài)下,點擊刪除按鈕刪除,關(guān)閉編輯狀態(tài)下,實現(xiàn)該方法,左滑進行刪除)
- (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;
}
//執(zhí)行重新排序的操作
- (void)tableView:(UITableView*)tableViewmoveRowAtIndexPath:(NSIndexPath*)sourceIndexPathtoIndexPath:(NSIndexPath*)destinationIndexPath{
? ? // 取出要拖動的模型數(shù)據(jù)
? ? NSString*letter = [NSStringstringWithFormat:@"%@",self.letterArr[sourceIndexPath.row]];
? ? //刪除之前行的數(shù)據(jù)
? ? [self.letterArr ?removeObject:letter];
? ? // 插入數(shù)據(jù)到新的位置
? ? [self.letterArr ?insertObject:letterat ?Index:destinationIndexPath.row];
}
第二種方法:長按手勢
1、給tableview添加長按手勢
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressRecognizer:)];
[self.tableView addGestureRecognizer:longPress];
2、手勢實現(xiàn)方法
//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;
}