2013-05-13 46 views
0

我有一個名爲「費用」的核心數據實體,其屬性爲「日期,類別,金額..........」。如何根據日期屬性中的「年 - 月」列出具有索引列表的所有費用?如何使用核心數據,iOS開發索引列表表視圖Dev

從蘋果的文檔和教程,我知道如何做索引列表表視圖。但它需要提供「陣列數組」。這是所有部分的一個數組,以及一個特定部分中的對象的子數組。現在我的數據結構不是這樣的。

從我的思想,我所能做的是:

  1. 首先獲取所有費用,然後提取「年 - 月」的所有情況下,沒有重複,把它們放到一個數組,這是所有該部分
  2. 然後我取基於每節

的全部費用,但我認爲這是一種繁重的工作,我可以更方便地實現這一目標?或者我應該改變我的數據結構? 謝謝你們:)

回答

1

您可以創建只返回費用在給定範圍內的請求:

//Not tested 
NSDateComponents* comps = [[NSDateComponents alloc] init]; 
NSCalendar* cal = [NSCalendar currentCalendar]; 
NSInteger year,month;//set these as you like 
[comps setYear:year]; 
[comps setMonth:month]; 
NSDate* start = [cal dateFromComponents:comps]; 
month += 1; 
month = (month <= 12 ? : 1); 
[comps setMonth:month]; 
NSDate* end = [cal dateFromComponents:comps]; 
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"date > %@ AND date < %@",start,end]; 
NSFetchRequest* request = [[NSFetchRequest alloc] initWithEntityName:@"Expense"]; 
[request setPredicate:predicate]; 

然後用NSFetchedResultsController來填充表視圖

+0

感謝您的回答,但從您的解決方案中,您知道獲取數據之前的年份和月份。但是首先應該從數據中讀取年份和月份。不是嗎? :)無論如何,我認爲我可以做到,非常感謝! – JimZ 2013-05-14 00:32:44

0

這是一個非常一般的問題,但基本的想法是使用NSFetchedResultsController顯示數據和NSPredicate進行過濾。

我還強烈建議你看看Sensible TableView這樣的框架,因爲它可以幫助你自動顯示和過濾盒子。