1
假設一個名爲TestTable的表:NSFetchedResultController。計算與表達sectionNameKeyPath /按條件
(NSNumber*) numValue
(NSString*) stringValue
然後在它假定約50條記錄。每個記錄中有不同的numValue
。 如果我創建NSFetchedResultController這樣的:
NSFetchRequest *request = [[NSFetchedResultsController alloc]
initWithFetchRequest: request
managedObjectContext: context
sectionNameKeyPath: @"numValue"
cacheName: @"testTable"]];
我會得到50段即可。
我的問題是: 我怎樣才能得到2個部分: 比一些x
numValue
以下且第2與記錄,其中numValue
比同x
更大含有記錄第一。
對於x = 15
它可能看起來像這樣:
NSFetchRequest *request = [[NSFetchedResultsController alloc]
initWithFetchRequest: request
managedObjectContext: context
sectionNameKeyPath: @"numValue > 15"
cacheName: @"testTable"]];
可能的解決方案是:
- 我們可以修改我們的實體@Dima說。
- 在
numberOfSectionsInTableView:
我們可以只看章節名稱並將其與x
進行比較。然後我們可以在tableView:numberOfRowsInSection:
中查看這些名稱並計算我們需要的。但那也感覺很髒。比第一種方法更好,但仍然。
UPDATE: 嗯,我幾乎想通了第3的方式來做到這一點。感謝@ Dima的鏈接。 下面是代碼:
[self setFetchedResultsController: [[NSFetchedResultsController alloc]
initWithFetchRequest: request
managedObjectContext: context
sectionNameKeyPath: [NSString stringWithFormat: @"numValue.compare(%f)", x]];
但與KVC遵守一個問題: 方法(選擇)與參數之間沒有明顯的KVC兼容。 解決方法的任何想法?
我不得不在OP中說我想要另一種方式,因爲改變實體對於這樣的事情感覺像是一種骯髒。 – folex 2012-07-12 07:53:45