TableView編輯操作

1. 添加操作

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

點(diǎn)擊cell前面的綠色小+ 會(huì)執(zhí)行commitEditingStyle 方法中的操作

2. 刪除操作

  1. 設(shè)置tableView 處于編輯狀態(tài)(側(cè)滑也可以出發(fā)刪除操作)
[self.tableView setEditing:YES animated:YES];
  1. 設(shè)置tableView的編輯類型為UITableViewCellEditingStyleDelete
  2. 在提交操作中刪除數(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. 選擇操作

  1. 設(shè)置tableView 處于編輯狀態(tài)(側(cè)滑也可以出發(fā)刪除操作)
[self.tableView setEditing:YES animated:YES];
  1. 設(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)

  1. 設(shè)置tableView 處于編輯狀態(tài)(側(cè)滑也可以出發(fā)刪除操作)
[self.tableView setEditing:YES animated:YES];
  1. 設(shè)置允許那些行移動(dòng)
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

  1. 交換數(shù)據(jù)
   NSString *temp = _sectionZeroArray[sourceIndexPath.row];
        _sectionZeroArray[sourceIndexPath.row] = _sectionZeroArray[destinationIndexPath.row];
        _sectionZeroArray[destinationIndexPath.row] = temp;
  1. 對(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. 拖拽

  1. 設(shè)置tableView 的drag 和 drop delegate
    self.tableView.dragDelegate = self;
    self.tableView.dropDelegate = self;
  1. 設(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];
}
  1. 返回拖拽預(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;
}
  1. 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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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