4

いくつかのセクションがあるテーブルビューがあります。あるセクションから別のセクションに行を移動し、行がなくなったらセクションを削除できるようにしたいと思います。moveRowAtIndexPathを使用してこれを実行しようとしていますが、コードが機能せず、NSRangeException例外がスローされます。

コードサンプルは次のとおりです。

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {

    NSUInteger fromSection = [fromIndexPath section];
    NSUInteger fromRow = [fromIndexPath row];
    NSString *fromKey = [self.keys objectAtIndex:fromSection];
    NSMutableArray *fromEventSection = [self.eventsDict objectForKey:fromKey];

    NSUInteger toSection = [toIndexPath section];
    NSUInteger toRow = [toIndexPath row];
    NSString *toKey = [self.keys objectAtIndex:toSection];
    NSMutableArray *toEventSection = [self.eventsDict objectForKey:toKey];

    id object = [[fromEventSection objectAtIndex:fromRow] retain];
    [fromEventSection removeObjectAtIndex:fromRow];
    [toEventSection insertObject:object atIndex:toRow];
    [object release];
    // The above code works just fine!

    // Try to delete an empty section. Here is where trouble begins:
    if ((fromSection != toSection) && [fromEventSection count] == 0) {
        [self.keys removeObjectAtIndex:fromSection];
        [self.eventsDict removeObjectForKey:fromKey];

        [tableView deleteSections:[NSIndexSet indexSetWithIndex:fromSection] withRowAnimation:UITableViewRowAnimationFade];
    }
4

2 に答える 2

3

削除をdispatch_asyncでラップすることにより、moveRowAtIndexPathメソッドの終了に続くブロックでdeleteSectionsメソッドを実行することができました。

    dispatch_async(dispatch_get_main_queue(), ^{
        if ((fromSection != toSection) && [fromEventSection count] == 0) {
            [self.keys removeObjectAtIndex:fromSection];
            [self.eventsDict removeObjectForKey:fromKey];
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:fromSection] withRowAnimation:UITableViewRowAnimationFade];
        }
    });
于 2012-07-31T14:37:10.713 に答える
0

これは私にもいくつかの悲しみを与えました。遅延を使用してセクションの削除を実行することに成功しました。

これを機能させる方法は次のとおりです-すべてのオブジェクトを格納するためにストアを使用していて、ストアにアイテムを移動するメソッドがあると仮定します:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    NSInteger beforeSectionCount = [store sectionCount];
    [store moveObject:fromIndexPath toIndexPath:toIndexPath];
    if (beforeSectionCount > [store sectionCount]
        [self performSelector:@selector(deleteSection:) withObject:fromIndexPath: afterDelay:0.2]
}

- (void)deleteSection:(NSIndexPath *)indexPath {
    [[self tableView] beginUpdates];
    [[self tableView] deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]]
                withRowAnimation:UITableViewRowAnimationFade];
    [[self tableView] endUpdates];
}
于 2013-08-27T01:27:44.470 に答える