iOS_UI_11_UITableView的編輯

第十一章 UITableView的編輯

一、UITableView編輯
1.UITableView編輯步驟
    1.打開編輯狀態(tài)
       - (void)setEditing:(BOOL)editing animated:(BOOL)animated{
           //一步:要打開tableView的編輯狀態(tài),我們首先要獲取tableView對象
           UITableView* mTableView = [self.view viewWithTag:1000];
           [mTableView setEditing:editing animated:animated];
           [mTableView reloadData];
       }
    2.協(xié)議設(shè)定
        1.確定Cell是否處于編輯狀態(tài)
           - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
        2.設(shè)定Cell的編輯樣式(刪除、添加)
           -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
        3.編輯狀態(tài)進(jìn)行提交
           - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
               if (editingStyle == UITableViewCellEditingStyleDelete) {
                   //說明做的事刪除操作
                   NSLog(@"你進(jìn)行了刪除操作");
                   //將數(shù)組中對應(yīng)位置的數(shù)據(jù)干掉
                   [self.allDataMArray removeObjectAtIndex:indexPath.row];
                   //界面刷新  當(dāng)數(shù)據(jù)源發(fā)生改變的時(shí)候我們需要刷新界面,讓新的數(shù)據(jù)重新顯示到界面上
           //        [tableView reloadData];//刷新整個(gè)視圖
                  //刪除對應(yīng)位置的單元格
                  [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationRight)];
    
    
                }
               if (editingStyle == UITableViewCellEditingStyleInsert) {
               //說明做的是插入操作
               NSLog(@"你進(jìn)行了插入操作");
               //先將數(shù)據(jù)添加到數(shù)組中
               [self.allDataMArray addObject:@"新增"];
               //增加單元格
                [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationTop)];
                }
           }
    注意:編輯結(jié)束后,由于numberOfRowsInSection這個(gè)協(xié)議只在tableView添加到父視圖的時(shí)候走一次,而且table上的數(shù)據(jù)都是由數(shù)組提供,因此,需要想將數(shù)組中的元素刪除,然后讓table的協(xié)議重新走一遍進(jìn)行重新賦值
          即:先修改數(shù)據(jù)源,再刷新table(使用reloadData方法)
2.UITableView的移動
    1.實(shí)現(xiàn)協(xié)議:告訴tableView是否能夠移動
        -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
    2.移動
        - (void)tableView:(UITableView *)tableView  moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
            NSLog(@"移動結(jié)束了");
            //將移動的單元格上的數(shù)據(jù)提出來,保存起來
            NSString* string = self.allDataMArray[sourceIndexPath.row];
            //刪除數(shù)組中的source位置的數(shù)據(jù)
            [self.allDataMArray removeObjectAtIndex:sourceIndexPath.row];
            //將數(shù)據(jù)插入到新的位置
            [self.allDataMArray insertObject:string atIndex:destinationIndexPath.row];
        }
3.設(shè)置某一行的編輯樣式
    - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
       if (indexPath.row == self.allDataMArray.count) {
           //說明是最后一行,讓最后一行的樣式插入
           return UITableViewCellEditingStyleInsert;
        }
        //其余行未刪除樣式
        return UITableViewCellEditingStyleDelete;
    }
4.設(shè)置某一行能否移動  返回值為YES:該cell可以移動
    indexPath:能否移動的行的位置
     - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
     //設(shè)置添加聯(lián)系人哪行不能移動
      if (indexPath.row == self.allDataMArray.count) {
       return NO;
      }
       return YES;
    }
5.移動過程中會執(zhí)行的代理方法
    sourceIndexPath:cell原位置
    proposedDestinationIndexPath:想要(可能)移動到的位置
    - (NSIndexPath*)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
        //如果cell將要移動到的位置時(shí)添加聯(lián)系人,那么我們就讓他返回原位置
        if (proposedDestinationIndexPath.row == self.allDataMArray.count) {
           return sourceIndexPath;
        }
           return proposedDestinationIndexPath;
     }
6.進(jìn)行編輯操作的按鈕樣式
    - (NSArray<UITableViewRowAction *>*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
      UITableViewRowAction* action_1 = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDefault) title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            [self.allDataMArray removeObjectAtIndex:indexPath.row];
            //界面刷新  當(dāng)數(shù)據(jù)源發(fā)生改變的時(shí)候我們需要刷新界面,讓新的數(shù)據(jù)重新顯示到界面上
            //        [tableView reloadData];//刷新整個(gè)視圖
            //刪除對應(yīng)位置的單元格
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationRight)];
            NSLog(@"點(diǎn)擊刪除按鈕");
        }];
        action_1.backgroundColor = [UIColor greenColor];
      UITableViewRowAction* action_2 = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@"更多" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            NSLog(@"點(diǎn)擊更多按鈕");
        }];
      UITableViewRowAction* action_3 = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleNormal) title:@"取消操作" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            NSLog(@"點(diǎn)擊取消按鈕");
      }];
      return @[action_1,action_2,action_3];
     }
二、UITableViewController
1.UITableViewController繼承自UIViewController,自帶一個(gè)tableView
2.self.view不是UIView而是UITableView
3.datasource和delegate默認(rèn)都是self(UITableViewController)
4.開發(fā)之需要建立UITableViewController子類
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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