ios UITableView左滑删除某行--关键代码

步骤:

一 .  允许删除的row或者Section返回YES,否则返回NO。

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
      if (_ljTableView == tableView)
      {
          //删除状态下,才允许左滑删除。
          if (_allArray.count > indexPath.section && [_attentionBtn.title isEqualToString:@"删除"])
          {
              Info *_ljInfo = _allArray[indexPath.section];
              if ([_ljInfo.Lineadd isEqualToString:@"1"] || [_ljInfo.Lineaddhint isEqualToString:@"1"])
              {
                  return NO;
              }
              return YES;//允许编辑
          }
      }
      return NO;//不允许编辑
}

二. 具体删除步骤

  1. 先移除tableView中数组的当前删除行  

   [_allArray removeObjectAtIndex:indexPath.section];

  2. deleteSections(删除某一个Section)  或者 deleteRowsAtIndexPaths(删除某一Row)

  // Delete the Sections from the data source.

  [tableViewdeleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]withRowAnimation:UITableViewRowAnimationAutomatic];

// Delete the row from the data source.

  [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade];



- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
     if (_ljTableView == tableView)
     {
         if (editingStyle == UITableViewCellEditingStyleDelete)
         {
             if(_allArray.count > indexPath.section)
             {
                 Info *_info = _allArray[indexPath.section];
                 [_allArray removeObjectAtIndex:indexPath.section];//1.先移除数组中的当前行
                 // Delete the deleteSections from the data source.//2.直接删除tableView中的当前行
                 [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
                // Delete the deleteRowsAtIndexPaths from the data source.
                //- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
             }
         }
     }
}


你可能感兴趣的:(IOS)