10

だから私はUITableViewビデオライブラリを表す を持っています。ライブラリのデータソースは、各キーNSMutableDictionaryの for を含むです。NSMutableArray各配列には、各値のすべての情報を含む配列が含まれています。

NSMutableDictionary-> を含む Foreach Key a NSMutableArray-> NSMutableArrays.

tableSectionData私のデータソースです。

最初のセクションに行を挿入し、別のセクションの別の行を削除しようとしています。これは私が使用しているコードです:

EDITこれは新しい試みです。最初にデータソースを更新してから、データソースでインデックス付けされた対応する行を追加および削除します。

[mainTableView beginUpdates];
    NSMutableDictionary *clone = [[NSMutableDictionary alloc] 
                          initWithDictionary:self.tableSectionData copyItems:YES];
    for (NSString *key in [clone allKeys]) {
        if([key isEqualToString:@"Videos available for download"]) {
            for (NSArray *videoArray in [clone objectForKey:key]) {
                if ([[videoArray objectAtIndex:0] isEqualToString:helper.videoName]) {
                    [[self.tableSectionData objectForKey:@"Downloaded videos"] 
                                               addObject:videoArray];
                    NSUInteger insertIndex = [[self.tableSectionData 
                                               objectForKey:@"Downloaded videos"] 
                                                        indexOfObject:videoArray];
                    NSIndexPath *pathToInsert = [NSIndexPath 
                                         indexPathForRow:insertIndex inSection:0];
                    NSArray *indexesToAddition = [NSArray 
                                                    arrayWithObject:pathToInsert];
                    [mainTableView insertRowsAtIndexPaths:indexesToAddition 
                                    withRowAnimation:UITableViewRowAnimationFade];
                    [[self.tableSectionData 
          objectForKey:@"Videos available for download"] removeObject:videoArray];
                    NSUInteger deleteIndex = [[self.tableSectionData 
         objectForKey:@"Videos available for download"] indexOfObject:videoArray];
                    NSIndexPath *pathToDelete = [NSIndexPath 
                                         indexPathForRow:deleteIndex inSection:1];
                    NSArray *indexesToDeletion = [NSArray arrayWithObject:
                                                                    pathToDelete];
                    [mainTableView deleteRowsAtIndexPaths:indexesToDeletion 
                                    withRowAnimation:UITableViewRowAnimationFade];

                }
            }
        }
    }
    [mainTableView endUpdates];

行を挿入したい場合は最初に行を削除する必要があるため、これはうまくいくと思いました(両方を行いたい場合)。

具体的なエラーは次のとおりです。

'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

エラーの内容はわかっていますが、セクションに対応する行数がありませんが、コードに間違いがなく、データソースをログに記録したところ、目的の結果に対応しています。

どんな助けや提案も大歓迎です!

4

3 に答える 3

13

begin/end 更新メソッド内ですべての更新を行う必要があります。複数の UITableView 更新がある場合は常に有効です。

endUpdates が呼び出される前に dataSource を更新することを忘れないでください。

[self.tableView beginUpdates];

    // do your staff here, like:
[self.tableView inserRowsAtIndexPaths:indexPathsToInsert];
[self.tableView deleteRowsAtIndexPaths:indexPathsToDelete];

[self.tableView endUpdates];

ここでの間違い - コードサンプルで異なる開始/終了更新セクションを使用していますか?

于 2013-02-19T14:02:13.800 に答える
4

Where is your dataSource?

before delete

[dataSource removeObjectAtIndex:nIndexDelete];
[mainTableView deleteRowsAtIndexPaths:indexesToDeletion 
                                     withRowAnimation:UITableViewRowAnimationFade];

before add

[dataSource inserObject:object atIndex:nIndexAdd];
[mainTableView insertRowsAtIndexPaths:indexesToAddition 
                                         withRowAnimation:UITableViewRowAnimationFade];
于 2013-02-19T15:17:09.167 に答える
-3

numberOfRowsInSection に正確な行数を返して行を挿入または削除するたびに UITableView をリロードする必要があると思います...

于 2013-02-19T13:13:44.010 に答える