[TwistedFate]UITableView表視圖編輯,移動

UITableView編輯

數據準備

//  初始化數據
- (void)initializeData{

    NSArray *array = @[@"0", @"1", @"2", @"3", @"添加"];
    NSArray *array2 = @[@"0", @"1", @"2", @"3", @"4", @"添加"];
    self.firstArray = [NSMutableArray arrayWithArray:array];

    self.secondArray = [NSMutableArray arrayWithArray:array2];

}

添加編輯按鈕

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"編輯" style:(UIBarButtonItemStylePlain) target:self action:@selector(barButtonClick:)];

self.navigationItem.rightBarButtonItem = barButtonItem;

[barButtonItem release];

開啓TableView的編輯狀態,按鈕實現方法

- (void)barButtonClick:(UIBarButtonItem *)barButtonItem{


    //   1.開啓編輯狀態
    [self.tableview setEditing:!self.tableview.editing animated:YES];

    //  編輯時更改標題
    if (self.tableview.editing == YES) {

        barButtonItem.title = @"完成";

    }else{

        barButtonItem.title = @"編輯";
    }

}

允許編輯

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    //  可以利用indexPath控制哪個分區的哪行  不能編輯
    return YES;

}

指定編輯的樣式

//  指定編輯的樣式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    //  返回編輯樣式
    if (indexPath.section == 0) {
        if ([self.firstArray[indexPath.row] isEqualToString:@"添加"]) {

            return UITableViewCellEditingStyleInsert;

        }

    }else{

        if ([self.secondArray[indexPath.row] isEqualToString:@"添加"]) {

            return UITableViewCellEditingStyleInsert;

        }

    }

    return UITableViewCellEditingStyleDelete;

}

根據編輯的樣式 和索引 去完成編輯

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {

        //  分區1
        if (editingStyle == UITableViewCellEditingStyleDelete) {

            //  刪除
            [self.firstArray removeObjectAtIndex:indexPath.row];

            //  刪除的刷新方法
            //  該方法 可以進行多行刪除 數值中填寫所有已經刪除的索引,可以填多個
            [self.tableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];

        }else {

            //  添加
            //  先操作數據
            [self.firstArray insertObject:@"緋村拔刀齋" atIndex:indexPath.row];

            //  添加刷新
            [self.tableview insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];

        }

        //  刷新界面
        //  整體刷新(UITableView)重走一遍數據源代理方法 達到刷新頁面的效果
        //  [self.tableview reloadData];

    }else {

        //  分區2
        if (editingStyle == UITableViewCellEditingStyleDelete) {

            //  刪除
            [self.secondArray removeObject:self.secondArray[indexPath.row]];

            //  刷新界面
            //  刪除
            [self.secondArray removeObjectAtIndex:indexPath.row];



        }else {

            //  添加
            [self.secondArray insertObject:@"浪客劍心" atIndex:indexPath.row];

            //  刷新界面
            [self.tableview insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];

        }

        [self.tableview reloadData];

    }

}

編輯步驟總結

  1. 開啓TableView的編輯狀態
  2. 允許哪個分區或者哪個分區的哪行是可以編輯的 (默認都能編輯)
  3. 指定可以編輯的樣式:刪除 or 添加
  4. 完成編輯 (提交編輯):
    - 操作數據源數組 (添加 或者 刪除)
    - 2.刷新UI界面

移動

開啓編輯狀態 與上面一樣,移動也是編輯的一種

允許哪個分區的哪一行 可以編輯canMove

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;

}

完成移動

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

    //  sourceIndexPath 來源的索引
    //  destinationIndexpath 目的地的索引

    //  移動的分類 (同分區移動, 跨分區移動)
    //  實際開發中禁止跨區移動

    //  判斷分區移動
    if (destinationIndexPath.section == sourceIndexPath.section) {

        //  同區
        if (sourceIndexPath.section == 0) {

            //  操作firstArray

            //  把來源索引下 數組對應的元素 保存一下
            NSString *str = self.firstArray[sourceIndexPath.row];

            //  用來源的索引 刪除數組中對應元素
            [self.firstArray removeObjectAtIndex:sourceIndexPath.row];

            //  插入到移動目的地索引處
            [self.firstArray insertObject:str atIndex:destinationIndexPath.row];

            //  移動刷新方法
            //  從哪裏來到哪裏去
            [tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath]
            ;

        }else{

            //  操作secondArray

            NSString *str = self.secondArray[sourceIndexPath.row];

            [self.secondArray removeObjectAtIndex:sourceIndexPath.row];

            [self.secondArray insertObject:str atIndex:destinationIndexPath.row];

            [tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];

        }

    }else{

        //  跨區

    }

}

限制跨區移動

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{

    //  sourceIndexPath 來源索引
    //  proposedDestinationIndexPath 建議目的地索引

    NSLog(@"我執行了");

    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {

        //  同區移動    可以返回 目的地索引
        return proposedDestinationIndexPath;

    }else{

        return sourceIndexPath;

        // 跨區移動 需要限制(從哪裏來 回哪裏去)
    }

}

移動步驟總結

  1. 開啓編輯狀態
  2. 允許哪個分區的哪一行 可以編輯canMove
  3. 完成移動(1.操作數據源數組 2.刷新UI界面)
發佈了68 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章