2011-04-18 42 views
1

我想創建一個導航界面,用戶可以點擊某個單元格並使新導航控制器與上一個導航控制器相同。我的管理對象具有以下結構:使用管理對象中的一組數據作爲獲取結果控制器的數據

name (string) 
orderId (int) 
orderBy (string, a key path indicating what to order the table with) 
dateCreated (date) 
items (a relationship pointing to the items for the next table) 

輕敲項目與非零項,下一個控制器獲取的輕敲的項目的引用,並使用它的「項目」,「排序依據」和「訂單ID」構造一個獲取的結果控制器(其項目爲數據)和一個排序描述符(使用orderBy和OrderId)。

如何告訴提取的結果控制器使用由項返回的NSSet作爲其數據?我可以使用謂詞將結果限制爲只有一個對象的項目嗎?謝謝

回答

1

您的項目需要有一個反向的多對一關係。讓我們稱這種關係爲「父母」。

在您的tableView委託的didSelectRowAtIndexPath中,啓動一個新的視圖控制器,並在所述indexPath處傳遞「父」對象。您將需要創建一個訪問器來在下一個視圖控制器中保存該對象。

正如你所猜測的,你可以做一個謂詞,以便你的fetchedResultsController只返回所說的「items」。使您的獲取結果只搜索「項目」實體。

NSPredicate *resultsPredicate = [NSPredicate predicateWithFormat:@"parent == %@", parentObject]; 
[fetchRequest setPredicate:resultsPredicate]; 

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] 
    initWithKey:[parentObject valueForKey:@"orderBy"] ascending:YES]; 
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
[fetchRequest setSortDescriptors:sortDescriptors]; 

基本上,您正在搜索所有與您選擇的對象具有父項關係的「項目」。

讓我知道我是否正確描述它(抱歉,如果我沒有,但我自己做這個,並可以指向一個蘋果的例子)。

編輯:蘋果示例代碼

http://developer.apple.com/library/ios/#samplecode/CoreDataBooks/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008405

http://developer.apple.com/library/ios/#samplecode/iPhoneCoreDataRecipes/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008913

+0

謝謝,非常完美。你能否將鏈接發佈到Apple示例代碼,以便我可以學習他們的技術。 – 2011-04-18 10:12:41

+0

抱歉,延遲。我已添加鏈接。 – 2011-04-21 07:11:01

+0

太好了,再次感謝。 – 2011-04-21 10:37:15

相關問題