2013-06-04 23 views
0

我正在爲管理團隊的職位分配的應用程序創建表視圖控制器。例如,具有防守,中鋒和進攻頭的部分將在其中具有名稱,如果位置實體存在於該位置屬性中。如果通過刷卡移除該人員,他們將成爲具有相同職位屬性的替代實體。在FetchedResultsController中獲取對象的indexPath

我試圖在點擊編輯按鈕時顯示每個位置的替代項。就像編輯聯繫人應用程序中的聯繫人時出現額外的聯繫人詳細信息一樣。

我有一個fetchedResultsController返回由positionProperty鍵定義的alts/positions的父實體來定義節。 (這可能是這樣做的錯誤方式...我是Core Data的新手)。

在setEditing:WithAnimation中我做了以下工作。試圖搜索我提取的結果,如果有任何對象是Alternate類型,則顯示該行。所以在枚舉中,如果它是交替類型,我試圖調用IndexPathForObjects:alt。這只是返回零...

if(editing){ 

     [self.tableView beginUpdates]; 

     for (MCAlternate *alt in fetchedResultsController.fetchedObjects) { 
      if ([alt isKindOfClass:[MCAlternate class]]) { 
       NSLog(@"The alternate is: %@", alt); 

       // This is where the error is trying to get indexPathForObject 
       NSIndexPath *index = [fetchedResultsController indexPathForObject:alt]; 
       [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[index] withRowAnimation:UITableViewRowAnimationLeft]; 
      } 
     } 
     [self.tableView endUpdates]; 
    } 

我已檢查結果中是否存在對象。它的確如此。我也試着在對象剛剛通過調用objectAtIndexPath創建的地方嘗試調用getindexpath,它仍然返回nil。

任何建議表示讚賞,謝謝!

回答

0

也許你沒有按照預見的那樣使用你提取的結果控制器。您的frc應該獲取所有數據以填充表格視圖。

您仍然可以通過調整表格視圖datasource方法來實現動態表格內容。假設您正在提取實體「位置」,並且所討論的位置對象具有稱爲「交替」的實體「MCAlternate」的一對多屬性。這樣,你會展開一定的部分,如下所示:

-(void)expandSectionForPosition:(Position *)position { 
    int row = 0; 
    int section = [[frc indexPathForObject:position] section]; 
    for (MCAlternate *alt in position.alternates) { 
     // update your datasource - e.g. by marking the position as 
     // "expanded"; make sure your numberOfRowsInSection reflects that 

     [_tableView insertRowsAtIndexPaths:@[[NSIndexPath 
      indexPathForRow:i++ inSection:section]] 
      withRowAnimation:UITableViewRowAnimationLeft]; 
    } 
} 

你的類檢查不符合邏輯 - 你是無論如何鑄塑MCAlternate在for循環。您的呼叫或獲取索引路徑失敗,因爲您通常沒有單獨的提取結果控制器。

+0

我不知道爲什麼我沒有想到從一個位置到另一個位置有多重關係!我知道我的模型並不理想,但無法找出最佳方法。我會去那。 至於我對MCAlternate的檢查......快速枚舉正在返回FRC中的所有內容(包括position和alts)。我覺得這很奇怪,但我想通過班級檢查可以解決這個問題。 感謝您的CD模型建議。 – WCByrne

相關問題