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

时间:2013-11-26 01:27:25

标签: ios objective-c uitableview

我目前有一个包含许多部分的tableview。每个部分包含特定日期的数据。每个部分都有一个标题,说明数据输入的天数。

在使用deleteRowsAtIndexPath删除行时,删除该部分中的所有行后,我尝试删除部分标题。

当遇到问题时,我首先从填充tableview的数组中删除数据,然后刷新tableview以在删除项目后更新视图。这有一个问题,即视图重新加载时出现了可怕的生涩过渡。

我决定使用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];

}

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

1 个答案:

答案 0 :(得分:1)

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

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

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