0

3つのセクションを持つUITableViewがあり、2番目のセクションには編集モードでの挿入と削除のインジケーターを示すテーブルがあります。cellForRowAtIndexPathに挿入行のセルを追加しています:編集がYESの場合。また、テーブルが編集モードになると、セクションの数を減らして3番目のセクションが表示されないようにします(編集モードのときに非表示にするボタンがあります)。setEditingで[self.tableViewreloadData]を呼び出さない限り、挿入行は表示されませんが、呼び出すとアニメーションが表示されません。私は何が間違っているのですか?

- (void)setEditing:(BOOL)flag animated:(BOOL)animated

{
  [super setEditing:flag animated:YES];
  [self.tableView setEditing:flag animated:YES];
  //unless i add [self.tableView reloadData] i don't see the + row, but then there is no animation
  [self.tableView reloadData];

セクションの数を決定するために私はこれを行っています

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.editing ? 2 : 3;
}

挿入行を追加するには、cellForRowAtIndexPathでこれを実行しています

 if (indexPath.row == [[[self recipe] tasks] count])
 {
    cell.textLabel.text = @"Add task...";
    cell.detailTextLabel.text = @"";

どんな助けでも大歓迎です。これにどれだけの時間を無駄にしたかを言うのは恥ずかしいです!

4

2 に答える 2

2

UITableViewの更新メソッドを使用する必要があります。詳細については、このテーマに関するAppleの包括的なガイドを確認してください。ただし、このコードスニペットからアイデアが得られるはずです。テーブルビューが編集モードを終了するときは、逆の操作を行う必要があることに注意してください。

NSIndexPath *pathToAdd = [NSIndexPath indexPathForRow:self.recipe.tasks.count section:SECTION_NEEDING_ONE_MORE_ROW];
NSIndexSet *sectionsToDelete = [NSIndexSet indexSetWithIndex:SECTION_TO_DELETE];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[ pathToAdd ] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView deleteSections:sectionsToDelete withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
于 2012-08-31T02:53:07.453 に答える
0

カール、どうもありがとう。完全。Appleのドキュメントを数回読んだことがありますが、理解できませんでした。グーグルの例は私を間違った道に送りました。問題は解決しました、そしてそれは本当に素晴らしく見えます。:)

NSIndexPath *pathToAdd = [NSIndexPath indexPathForRow:self.recipe.tasks.count section:1];
NSIndexSet *sectionsToDelete = [NSIndexSet indexSetWithIndex:2];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[ pathToAdd ] withRowAnimation:UITableViewRowAnimationAutomatic];
// update the datasource to reflect insertion and number of sections
// I added a 'row' to my datasource for "Add task..."
// which is removed during setEditing:NO
[self.tableView deleteSections:sectionsToDelete withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];

トッド

于 2012-08-31T20:35:23.277 に答える