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<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
             }
         }
     }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值