iosTableView 局部全部重新整理以及刪除編輯操作

來源:互聯網
上載者:User

標籤:com   win   code   mat   nbsp   default   代理   cte   actions   

  • 局部重新整理方法

    1. 添加資料

      NSArray *indexPaths = @[                       [NSIndexPath indexPathForRow:0 inSection:0],                       [NSIndexPath indexPathForRow:1 inSection:0]                       ];[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];
    2. 刪除資料

      NSArray *indexPaths = @[                     [NSIndexPath indexPathForRow:0 inSection:0],                     [NSIndexPath indexPathForRow:1 inSection:0]                     ];[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];
    3. 更新資料(沒有添加和刪除資料,僅僅是修改已經存在的資料)

      NSArray *indexPaths = @[                     [NSIndexPath indexPathForRow:0 inSection:0],                     [NSIndexPath indexPathForRow:1 inSection:0]                     ];[self.tableView relaodRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];
  • 左滑出現刪除按鈕

    • 需要實現tableView的代理方法

        // 只要實現了這個方法,左滑出現Delete按鈕的功能就有了  // 點擊了“左滑出現的Delete按鈕”會調用這個方法  - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath      {      // 刪除模型      [self.wineArray removeObjectAtIndex:indexPath.row];      // 重新整理      [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];      }          // 修改Delete按鈕文字為“刪除”      - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath      {          return @"刪除";      }
  • 左滑出現N個按鈕

    • 需要實現tableView的代理方法只要實現了這個方法,左滑出現按鈕的功能就有了(一旦左滑出現了N個按鈕,tableView就進入了編輯模式, tableView.editing = YES)

      - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  {  }     // 左滑cell時出現什麼按鈕  - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath  {      UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"關注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {      NSLog(@"點擊了關注");      // 收回左滑出現的按鈕(退出編輯模式)      tableView.editing = NO;  }];  UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {      [self.wineArray removeObjectAtIndex:indexPath.row];      [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];  }];  return @[action1, action0];}
  • 進入編輯模式

// self.tabelView.editing = YES;[self.tableView setEditing:YES animated:YES];// 預設情況下,進入編輯模式時,左邊會出現一排紅色的“減號”按鈕
  • 在編輯模式中多選
// 編輯模式的時候可以多選self.tableView.allowsMultipleSelectionDuringEditing = YES;// 進入編輯模式[self.tableView setEditing:YES animated:YES];// 獲得選中的所有行self.tableView.indexPathsForSelectedRows;

 

iosTableView 局部全部重新整理以及刪除編輯操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.