2

一旦编辑模式开始,我想更改 UITableView 显示的删除文本。

委托方法:

 -(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath 

仅当第一次显示索引路径处的 deleteButton 时才调用,但如果我的模型在它下面发生更改,我需要更新此文本。是否可以在不重新加载整个部分的情况下再次调用此方法?请参阅下面的代码,并提前感谢您的帮助。

    -(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
      ContainerTableViewCell *cell = (ContainerTableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];

      if ([cell.editPhotos count] > 0) {
          return [NSString stringWithFormat:@"Delete %d photos", [cell.editPhotos count]];
      } 
      else{
          return @"Delete Section";
      }
   }

在某些上下文中,我有一个嵌套在uiatevieviewcell中的UicollectionView,选择单元格时将发送通知。我尝试使用以下内容重新加载该部分:

 [self.tableView reloadRowsAtIndexPaths:@[[self.tableView indexPathForCell:cell]] withRowAnimation:UITableViewRowAnimationNone];

但这是不可取的,因为它会导致 tableview 跳转并且无法正确显示选择。我也试过:

 [self.tableView.delegate tableView:self.tableView titleForDeleteConfirmationButtonForRowAtIndexPath:[self.tableView indexPathForCell:cell]];

绝望中。虽然这确实会导致调用正确的方法,但它不会更改删除文本。

4

2 回答 2

0

我找到了解决这个问题的方法,尽管它有点小技巧。委托方法

     -(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath 

每次 UITableView 进入编辑模式时,每个单元格都会调用一次。因此,为了在数据更改时更改标题,我切换了编辑模式,使用布尔值表示我希望保存选定的信息,即:

        cell.retainEditSelection = YES; 
        [self.tableView setEditing:NO animated:NO];
        [self.tableView setEditing:YES animated:NO];
        cell.retainEditSelection = NO;

每次选择应该更改我的删除文本的内容时,我都会使用它。希望这可以帮助 。

于 2013-09-10T19:57:44.110 回答
0

我刚刚编写了一个基本的测试应用程序,它可以按预期工作。

我认为也许你获取数据的方式不是最好的方法。您正在查询一个可能已出队的单元格,因此可能不包含最新信息。

相反,您应该努力实现真正的 MVC 模式,其中您的数据独立于您的视图,包括集合视图单元格。

于 2013-09-08T18:52:04.340 回答