iOS TabelViewCell 刪除 編輯 插入

/** TableView 進入或退出編輯狀態(TableView 方法). */ - (void)setEditing:(BOOL)editing animated:(BOOL)animate{

    /*首先調用父類的方法*/
    [super setEditing:editing animated:animated];
    /*使tableView出於編輯狀態*/
    [self.tableView setEditing:editing animated:animated];
}
 
/** 肯定哪些行的cell能夠編輯 (UITableViewDataSource協議中方法). */
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
if (0 == indexPath.row)  {
      return No;  /*第一行不能進行編輯*/
   } else {
      return Yes;
   }
}
 
/** 設置某一行cell的編輯模式 (UITableViewDelegate協議中方法). */
- (TableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
  
  if (indexPath.row > 10){
    return UITableViewCellEditingStyleInsert;
    }else{
    return  UITableViewCellEditingStyleDelete;
  }
}
 
/** 提交編輯狀態 (UITableViewDataSource協議中方法). */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
  
    /** 點擊 刪除 按鈕的操做 */
    if (editingStyle == UITableViewCellEditingStyleDelete) {
    /**< 判斷編輯狀態是刪除時. */ /** 1. 更新數據源(數組): 根據indexPaht.row做爲數組下標, 從數組中刪除數據. */
    [self.arr removeObjectAtIndex:indexPath.row];
    /** 2. TableView中 刪除一個cell. */
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }
    /** 點擊 +號 圖標的操做. */
    if (editingStyle == UITableViewCellEditingStyleInsert) {
      /**< 判斷編輯狀態是插入時. */ /** 1. 更新數據源:向數組中添加數據. */
      [self.arr insertObject:@"abcd" atIndex:indexPath.row]; /** 2. TableView中插入一個cell. */
      [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
   }
}
 
/** 提交編輯狀態 (UITableViewDataSource協議中方法). */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 
/** 插入 cell (UITableView 方法). */ - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
 
/** 刪除 cell (UITableView 方法). */ - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation


上面的需求思路:
數組

        1.tableView 要進入可編輯狀態(根據本身的須要)spa

        2.更具本身須要,刪除,或者,插入,指定Cellcode

        3.最後對數據進行操做,刷新頁面rem

相關文章
相關標籤/搜索