ios - 如何在使用 deleteRowsAtIndexPaths 删除行时删除节标题

标签 ios objective-c uitableview

我目前有一个包含多个部分的表格 View 。每个部分包含特定日期的数据。每个部分都有一个标题,说明数据是在多少天前输入的。

当使用deleteRowsAtIndexPath删除行时,当该部分中的所有行都被删除后,我试图删除部分标题。

当遇到这个问题时,我首先从填充 TableView 的数组中删除数据,然后在删除项目后刷新 TableView 以更新 View 。这存在一个问题,即 View 重新加载时出现可怕的不稳定过渡。

我决定使用deleteRowsAtIndexPaths,它运行良好且顺利。这就带来了一个问题,即当您删除某个部分中的所有行时,标题仍然保留。

这就是问题的核心。

如果我使用deleteRowsAtIndexPaths,那么动画效果很好,但之后我无法刷新表格,因为它会破坏动画。但如果我使用它,那么删除后我会留下部分标题。

如果我刷新,那么部分标题将会消失,但动画对用户来说很糟糕。

这是我目前使用的代码:

// This is the method done when we go into slide to delete editing
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

  // This allows us to calculate what row was clicked rather than the section and row
  NSUInteger row = 0;
  NSUInteger sect = indexPath.section;
  for (NSUInteger i = 0; i < sect; ++ i)
      row += [self tableView:tableView numberOfRowsInSection:i];
      row += indexPath.row;

  // This gets us the exercise event that has been clicked

  BExerciseEvent * exerciseEvent = _exercise.exerciseEvents[row];

  NSInteger j;

  for (j = 0; j < _exercise.exerciseEvents.count; j++) {
      BExerciseEvent * event = _exercise.exerciseEvents[j];

      if ([exerciseEvent isEqual:event]) {

          // Delete from Database
          [event deleteAllParametersForExerciseEvent];
          [event deleteRow];

          // Delete correct rows from array
          _exerciseEvent = _sections[(indexPath.section - 1)][indexPath.row];
          [_sections[(indexPath.section - 1)] removeObject: _exerciseEvent];

          // Delete from table view
          [tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:(indexPath.section)]] withRowAnimation:UITableViewRowAnimationTop];

      }
  }

  [super setEditing:NO animated:YES];
  [self.tableView setEditing:NO animated:YES];
  [self.navigationController setToolbarHidden:YES animated:YES];

}

感谢您提供的任何帮助或建议

最佳答案

最好的选择是删除整个部分。当然,这意味着您的数据源需要设置,因此该部分确实消失了,而不仅仅是设置了 0 行。

您不必删除每一行,而是从数据源中删除整个部分,然后调用:

[tableView deleteSections:[NSIndexSet indexSetWithIndex:someSection] withRowAnimation:UITableViewRowAnimationTop];

关于ios - 如何在使用 deleteRowsAtIndexPaths 删除行时删除节标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20206916/

相关文章:

ios - 如何在 NSArray 中查找字符并在 Objective-C 中返回 YES?

ios - IOS 10.3 不支持某些泰米尔文字

iphone - UISearchDisplayController 和 UITableView 原型(prototype)单元格崩溃

swift - 什么时候调用 CellForRowAt? -- 排队直到 VC 排在最前面?

ios - 如何在不使用 GKPeerPicker 的情况下通过 GKSession 在手机之间传输数据

iphone - 使用企业分发配置文件控制谁使用 iOS 企业应用程序?

ios - 应用因 iOS 广告标识符而被拒绝

ios - Xcode 8 中的单元测试失败

objective-c - 在 OS X 中打开和创建窗口

iphone - UINavigationController popViewControllerAnimated : crash in iOS 6