2013-02-19 12 views
8

ビデオライブラリを表すUITableViewがあります。ライブラリのデータソースは、それぞれのキーにNSMutableArrayを含むNSMutableDictionaryです。各配列には、各値のすべての情報を含む配列が含まれています。行の挿入と行の削除を同時に行います。 UITableView

NSMutableDictionary - >NSMutableArraysを含むForeach KeyのNSMutableArray

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

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

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

[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]; 

私は、私は1つを挿入したい場合、私は最初の行を削除する必要があるため、これは働くだろうと思った

特定のエラーは以下の通りです(私は両方を行いたい場合。):

'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).' 

エラーの内容は分かりますが、セクションに対応する行数はありませんが、コードに間違いがなく、データソースが記録され、目的の結果に対応しています。

ご協力いただきありがとうございます。

答えて

-3

私は/終了のアップデート方法を始めるあなたは内部のすべての更新をしなければならない

+0

を追加します。私は開始と更新の更新に固執する! – Joze

9

...あなたはnumberOfRowsInSection内の行の正確な数を返すと行を挿入または削除するたびにのUITableViewリロードする必要があると思います。複数のUITableView更新がある場合は常に有効です。

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

[self.tableView beginUpdates]; 

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

[self.tableView endUpdates]; 

ここにあなたのミス - あなたのコードサンプルでは異なる開始/終了の更新セクションを使用しているされて

+0

コードのコメントと同じように、私はすでにこれを試していると述べているので、別のセクションでやってみたのです。これは問題ではありません。さらに、insertRowsAtIndexPathsはdeleteRowsAtIndexPathsの後に置かなければなりません。また、ドキュメントや既に作成したテストに従って動作しません。とにかくあなたの提案に感謝します。 – Joze

4

どこdataSourceのはありますか?削除

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

は前に私はすでにこれを試みたが、うまくいきませんでした

[dataSource inserObject:object atIndex:nIndexAdd]; 
[mainTableView insertRowsAtIndexPaths:indexesToAddition 
             withRowAnimation:UITableViewRowAnimationFade]; 
+0

こんにちは、ありがとう、私はすでにこれを(または私はそれを間違っている可能性が高い)私は私のdataSourceのクローンを反復最初のを見てください。私のdataSourceはtableSectionDataです。そこに問題のオブジェクトを追加して削除します。私がしようとしているのは、あるセクションの行を別のセクションに移すことです。私は自分のコードで間違いを見ることはありません... – Joze

+0

メソッド** numberOfSectionsInTableViewで使用するコードを知っているかもしれません:**と** tableView:numberOfRowsInSection:**? –

+0

私は、UITableViewDelegateのそれらの部分が正しいことを知っています。それは、更新が呼び出されている順番の問題です。 reloadDataメソッドは私のために働いたが、私はまだ挿入と削除が動作しない理由を理解していない。更新された質問を見てください。 – Joze

関連する問題