iOS tableviewcell上下拖動排序

有兩種方式可以實現(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;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容