0

私は、Appleが提供するコアデータブックのサンプルプロジェクトに基づいてアプリケーションを作成し、各セルに特定のデータを表示するカスタムセルを作成しました。コアデータブックの例カスタムセルの追加

しかし、今では2番目のカスタムセルを追加しようとしています。それは常にテーブルの最初のセルになる単一のセルです。そして、コアデータから取得された結果のすべてがそれ以下になります。

私はそうのようにしようとしている:いくつかの他の変更とともに

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    if (indexPath.row == 0) 
    { 
     static NSString *CellIdentifier = @"statsCell"; 

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

     //Configure the cell. 
     [self configureStatsCell:cell]; 

     return cell; 
    } 
    else 
    { 
     static NSString *CellIdentifier = @"guestCell"; 

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

     // Configure the cell. 
     [self configureGuestCell:cell atIndexPath:indexPath]; 

     return cell; 
    } 
} 

作業の事をしようとして取得します。いくつかの問題。

まず、ビューが正常にロードされます。カスタムセルがインデックス0に配置され、コアデータセルがその下にロードされます。最初の問題は、最初のコアデータセルがカスタムセルの背後に隠れているように見えないが、それをタップするとそのコアデータエントリの詳細ビューが表示される。だから私はどのように私はそれがカスタムセルの後ろに行くのを止めることができるかを考え出す必要があります。だから、

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section]; 
    return [sectionInfo numberOfObjects]; 
} 

、これが唯一のコアデータレコードのために十分な細胞を設定し、その追加のカスタムを考慮していない:私はこれを改善しようとしてい

は、例えば、このチャンクという論理的なようです細胞。

return [sectionInfo numberOfObjects] + 1; 

その後、これは私が境界エラーを超えクラッシュやインデックスを取得させ、この行になり:

GuestInfo *guest = [fetchedResultsController objectAtIndexPath:indexPath]; 

ラインがどのセットアップconfigureCell方法からなるように、私はこのことな単純なものを試してみてくださいコアデータ記録セル。だから、イムは何をすべきかに関してかなり困惑した。

次の問題は、詳細ビューでコアデータレコードの属性を編集してtableviewに戻るときに、カスタムセルに変更が反映されないことです。これは、コアデータ情報と関連する統計情報を表示します。他のセルだけが更新されます。

助けていただければ幸いです。もうコードを見たり、もっと説明が必要な場合は、私に教えてください。

編集:

他にもコードがあります。これはNSFetchedControllerコードです。

- (NSFetchedResultsController *)fetchedResultsController 
{ 

    if (fetchedResultsController != nil) 
    { 
     return fetchedResultsController; 
    } 

    // Create and configure a fetch request with the Book entity. 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"GuestInfo" inManagedObjectContext:managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    // Create the sort descriptors array. 
    NSSortDescriptor *lastNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES]; 
    NSSortDescriptor *firstNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES]; 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:lastNameDescriptor, firstNameDescriptor, nil]; 
    [fetchRequest setSortDescriptors:sortDescriptors]; 

    // Create and initialize the fetch results controller. 
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"displayOrder" cacheName:@"Root"]; 

    self.fetchedResultsController = aFetchedResultsController; 
    fetchedResultsController.delegate = self; 

    // Memory management. 
    //No releasing with ARC! 

    return fetchedResultsController; 
}  


/** 
Delegate methods of NSFetchedResultsController to respond to additions, removals and so on. 
*/ 

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
    // The fetch controller is about to start sending change notifications, so prepare the table view for updates. 
    [self.tableView beginUpdates]; 
} 


- (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 configureGuestCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 
      [self.tableView reloadData]; 
      break; 

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


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { 

    switch(type) { 

     case NSFetchedResultsChangeInsert: 
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates. 
    [self.tableView endUpdates]; 
} 
+0

を適用する方法ですあなたは 'ARC'を有効にするには?そうでなければ、-tableView:cellForRowAtIndexPath:によって返されたセルは自動リリースされるべきです。 – AechoLiu

+0

'NSFetchedResultController'を使用しました。したがって、fetchedResultControllerの設定に関するコードを投稿することはできますか?そして、[fetchedResultsController performFetch:...]に関するコード。ところで、管理対象オブジェクトの属性を変更した後、[tableView reloadData]を呼び出してテーブルビューを更新しますか? – AechoLiu

+0

私はARCを使用しています。 '[self.tableView reloadData]'を呼び出すと、単一のカスタムセルでデータをリロードする問題を解決できます。しかし、まだ奇妙なセルが重なり合っているかどうかを調べる必要があります。私が要求したコードを投稿します。 –

答えて

0

私は同じ機能を実装しており、indexPath @ Toroのソリューションでオフセットを使用しています。より良い方法があるかどうかわからない、興味のある人のための私の調整です。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; 
    return [sectionInfo numberOfObjects] + 1; 
} 

カスタムセルに追加の行を1つ追加すると、フェッチされた結果にセクションがないため、この作業はうまくいきます。

- (NSIndexPath *)adjustedIndexPath:(NSIndexPath *)indexPath 
{ 
    NSIndexPath *newPath = [NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section]; 
    return newPath; 
} 

NsFetchedResultControllerから右のオブジェクトを取得するindexpathを調整するための民間の方法。ここで

は私がadjustedIndexPath:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
関連する問題