2012-02-08 18 views
0

私のアプリケーションには、ルートコントローラとしてUITableViewControllerがあり、このテーブルに行を追加するためのモーダルビューがあります。 CoreDataを使用しているので、NSFetchedResultsControllerからデータを取得します。とにかく、問題は、UITableViewの管理にあります。アニメーションを含む最初の行に挿入すると、UITableViewがセルを正しく表示しない

各挿入により、TableViewは新しいセルを追加します。セルが最初の行に追加されている場合を除き、すべて正常に動作します。この場合、そのコンテンツは表示されません。セルに空白が表示されます。セルをタップするか、セルを再読み込みする必要があるようにテーブルをスクロールすると、コンテンツが表示されます。

私は[tableView beginUpdates]と[tableView endUpdates]を[docView reloadData]の代わりにApple docsが言ったようにアニメーションを使用しています。 [tableView reloadData]を実行するとすべて正常に動作します。

私はそれをチェックし、すべての行に対して同じコードが実行されています。細胞の表示方法は問題です。

私はこの問題はTableViewsでアニメーションの「理論」についてだと思う、あなたはおそらくそれを必要としませんが、私のUITableViewControllerの関連するコードがある:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 
    Customer *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    cell.textLabel.text = managedObject.name; 
} 


#pragma mark - Table view data source 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    [self configureCell:cell atIndexPath:indexPath]; 

    return cell; 
} 


#pragma mark - FetchedResultsController delegate 


- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
    [self.tableView beginUpdates]; 
} 

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
    [self.tableView endUpdates]; 
    // [self.tableView reloadData]; this make all works OK but without animations 
} 


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 
    atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type 
    newIndexPath:(NSIndexPath *)newIndexPath { 

UITableView *tableView = self.tableView; 

switch(type) { 

    case NSFetchedResultsChangeInsert: 
     [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     break; 

    case NSFetchedResultsChangeDelete: 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     break; 

    case NSFetchedResultsChangeUpdate: 
     [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 
     break; 

    case NSFetchedResultsChangeMove: 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade]; 
     break; 
} 
} 
+0

ケースNSFetchedResultsChangeInsertにブレークポイントを配置してコードをデバッグしましたが、newIndexPathを記録しようとしました。 –

答えて

0

は、各タイプのためにあなたのswitch声明で変更により、なぜ追加しない:

[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

をあなたは変更の種類に応じて、わずかにそのコードを変更する必要がありますが、これはbeginUpdatesendUpdatesの必要性を排除するであろう。

+0

さて、@colbyさん、ありがとうございますが、新しいオブジェクトの名前を調整するためにモーダルビューを表示するボタンをタップすると行が挿入されるので、モーダルビューから戻るときにこの行を置く必要がありました。問題。だから、私はさまざまな解決策があることを知っていますが、私はbegin/endUpdatesメカニズムについては愚かな問題だと確信しているので、コードをあまり変更したくありません。私は奇妙な何もしていない、このメカニズムだけを使用しています。それは動作する必要があります! :) – angeleke

関連する問題