1. 添加操作
- 添加一個(gè)按鈕讓tableView 進(jìn)入編輯狀態(tài)
- 設(shè)置cell在編輯狀態(tài)下的類型為
UITableViewCellEditingStyleInsert- 在提交操作里面(
tableView:commitEditingStyle:forRowAtIndexPath:)刷新數(shù)據(jù)源及表視圖(或單獨(dú)的行)

添加按鈕

cell編輯狀態(tài)類型為+
點(diǎn)擊cell前面的綠色小+ 會(huì)執(zhí)行commitEditingStyle 方法中的操作
2. 刪除操作
- 設(shè)置tableView 處于編輯狀態(tài)(側(cè)滑也可以出發(fā)刪除操作)
[self.tableView setEditing:YES animated:YES];
- 設(shè)置tableView的編輯類型為
UITableViewCellEditingStyleDelete - 在提交操作中刪除數(shù)據(jù)
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[self deleteCellAtIndexPath:indexPath];
}

刪除
8.0 之后側(cè)滑欄設(shè)置
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
[self deleteCellAtIndexPath:indexPath];
}];
deleteAction.backgroundColor = [UIColor magentaColor];
NSArray *array = [NSArray arrayWithObjects:deleteAction, nil];
return array;
}
11.0 后側(cè)滑欄設(shè)置
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos)
{
UIContextualAction *topAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"置頂" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
NSLog(@"置頂");
// 執(zhí)行完回調(diào)后側(cè)邊欄是否收回
completionHandler(YES);
}];
UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"刪除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
[self deleteCellAtIndexPath:indexPath];
completionHandler (true);
}];
deleteAction.backgroundColor = [UIColor redColor];
UISwipeActionsConfiguration *condig = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction,topAction]];
// 設(shè)置為NO,防止完整的滑動(dòng)動(dòng)作執(zhí)行第一個(gè)action的回調(diào)
condig.performsFirstActionWithFullSwipe = NO;
return condig;
}
3. 選擇操作
- 設(shè)置tableView 處于編輯狀態(tài)(側(cè)滑也可以出發(fā)刪除操作)
[self.tableView setEditing:YES animated:YES];
- 設(shè)置tableView的編輯類型為
UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert
- 全選操作
for (int i = 0; i < self.dataArray.count; i++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}
- 全不選
for (int i = 0; i < self.dataArray.count; i++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
注: [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]方法并不會(huì)觸發(fā)tableView的- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath代理方法。
4. 移動(dòng)
- 設(shè)置tableView 處于編輯狀態(tài)(側(cè)滑也可以出發(fā)刪除操作)
[self.tableView setEditing:YES animated:YES];
- 設(shè)置允許那些行移動(dòng)
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- 交換數(shù)據(jù)
NSString *temp = _sectionZeroArray[sourceIndexPath.row];
_sectionZeroArray[sourceIndexPath.row] = _sectionZeroArray[destinationIndexPath.row];
_sectionZeroArray[destinationIndexPath.row] = temp;
- 對(duì)移動(dòng)目標(biāo)地址添加限制
例:設(shè)置同一個(gè)源cell和目標(biāo)cell必須在同一section中才可移動(dòng)
// proposedDestinationIndexPath為即將移動(dòng)到的位置
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if(sourceIndexPath.section == proposedDestinationIndexPath.section)
{
return proposedDestinationIndexPath;
}
else
{
return sourceIndexPath;
}
}

移動(dòng)
5. 拖拽
- 設(shè)置tableView 的drag 和 drop delegate
self.tableView.dragDelegate = self;
self.tableView.dropDelegate = self;
- 設(shè)置可供拖拽的Item
- (nonnull NSArray<UIDragItem *> *)tableView:(nonnull UITableView *)tableView itemsForBeginningDragSession:(nonnull id<UIDragSession>)session atIndexPath:(nonnull NSIndexPath *)indexPath {
NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithObject:self.dataArray[indexPath.row]];
UIDragItem *item = [[UIDragItem alloc] initWithItemProvider:itemProvider];
self.dragIndexPath = indexPath;
return @[item];
}
- 返回拖拽預(yù)覽參數(shù)
- (nullable UIDragPreviewParameters *)tableView:(UITableView *)tableView dragPreviewParametersForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIDragPreviewParameters *parameters = [[UIDragPreviewParameters alloc] init];
CGRect rect = CGRectMake(0, 0, tableView.bounds.size.width, tableView.rowHeight);
parameters.visiblePath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:15];
return parameters;
}
- drop delegate
// 當(dāng)用戶開始初始化 drop 手勢的時(shí)候會(huì)調(diào)用該方法
- (void)tableView:(UITableView *)tableView performDropWithCoordinator:(id<UITableViewDropCoordinator>)coordinator {
NSIndexPath *destinationIndexPath = coordinator.destinationIndexPath;
// 如果開始拖拽的 indexPath 和 要釋放的目標(biāo) indexPath 一致,就不做處理
if (self.dragIndexPath.section == destinationIndexPath.section && self.dragIndexPath.row == destinationIndexPath.row) {
return;
}
[tableView performBatchUpdates:^{
// 目標(biāo) cell 換位置
id obj = self.dataArray[self.dragIndexPath.row];
[self.dataArray removeObjectAtIndex:self.dragIndexPath.row];
[self.dataArray insertObject:obj atIndex:destinationIndexPath.row];
[tableView deleteRowsAtIndexPaths:@[self.dragIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:@[destinationIndexPath] withRowAnimation:UITableViewRowAnimationFade];
} completion:^(BOOL finished) {
}];
}
// 該方法是提供釋放方案的方法,雖然是optional,但是最好實(shí)現(xiàn)。
// 當(dāng)跟蹤 drop 行為在 tableView 空間坐標(biāo)區(qū)域內(nèi)部時(shí)會(huì)頻繁調(diào)用
// 當(dāng)drop手勢在某個(gè)section末端的時(shí)候,傳遞的目標(biāo)索引路徑還不存在(此時(shí) indexPath 等于 該 section 的行數(shù)),這時(shí)候會(huì)追加到該section 的末尾;
// 在某些情況下,目標(biāo)索引路徑可能為空(比如拖到一個(gè)沒有cell的空白區(qū)域);
// 在某些情況下,你的建議可能不被系統(tǒng)所允許,此時(shí)系統(tǒng)將執(zhí)行不同的建議,可以通過 -[session locationInView:] 做你自己的命中測試;
- (UITableViewDropProposal *)tableView:(UITableView *)tableView dropSessionDidUpdate:(id<UIDropSession>)session withDestinationIndexPath:(nullable NSIndexPath *)destinationIndexPath
{
UITableViewDropProposal *dropProposal;
// 如果是拖拽到另外一個(gè)app,localDragSession為nil,此時(shí)就要執(zhí)行copy,通過這個(gè)屬性判斷是否是在當(dāng)前app中釋放,只有 iPad 才需要這個(gè)適配
if (session.localDragSession)
{
dropProposal = [[UITableViewDropProposal alloc] initWithDropOperation:UIDropOperationMove intent:UITableViewDropIntentInsertAtDestinationIndexPath];
}
else
{
dropProposal = [[UITableViewDropProposal alloc] initWithDropOperation:UIDropOperationCopy intent:UITableViewDropIntentInsertAtDestinationIndexPath];
}
return dropProposal;
}