0

このコードをtableViewに実装しています。

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return NO;
    }
    return YES;
}

それは私が望むことをしますが、私は一歩進んで、編集ボタンが押されたときに「セクション0」が完全に消えるようにしたいです(この効果は、iOSの「キーボード」メニューに移動して編集を選択すると表示されます右上隅、アニメーションでは上の2つのセクションが消えます)。最初のセクションを一時的に削除しようとしましたが、[tableView reloadData];が呼び出されるとアプリがクラッシュします。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tvController.editing == YES) {
        return 1;
    }else if (tvController.editing == NO) {
        return 2;
    }
    return 0;
}

また、そのコードを機能させてもアニメーションになってしまうとは思いません。私のアプローチは間違っていると思います。助けてくれてありがとう!

4

1 に答える 1

1

あなたの問題

セクションの 1 つが前のセクションよりも長くなっています。

で 1 つ少ないセクションをレポートすることでセクション 0 を非表示にするためnumberOfSectionsInTableView:、編集モードではすべてのデリゲート メソッドでセクション番号を調整する必要があります。それらの1つはそうしていません。

// every delegate method with a section or indexPath must adjust it when editing

- (NSInteger) tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tvController.editing) section++;
    return [[customers objectAtIndex:section] count];
}

- (UITableViewCell*) tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath
{
    int section = indexPath.section;
    if (tvController.editing) section++;

    id customer = [[customers objectAtIndex:section] indexPath.row];

    // etc
}

私のアプローチ

UITableView reloadSections:withRowAnimation:指定されたセクションをアニメーションでリロードします。setEding:animated:デリゲート メソッドから呼び出します。

- (void) setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    UITableViewRowAnimation animation = animated ? UITableViewRowAnimationFade : UITableViewRowAnimationNone;
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:animation];

    [self.tableView reloadSectionIndexTitles];

    self.navigationItem.hidesBackButton = editing;
}

デリゲートは、非表示セクションに行やタイトルがないことも示す必要があります。

- (NSInteger) tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.editing && section == 0) {
        return 0;
    }

    return [[customers objectAtIndex:section] count];
}

- (NSString*) tableView:(UITableView*) tableView titleForHeaderInSection:(NSInteger) section
{
    if (self.editing && section == 0) {
        return nil;
    }

    [[customers objectAtIndex:section] title];
}
于 2012-07-04T15:26:20.030 に答える