uitableview - How to make index list table view with Core Data, iOS Dev -
i have entity in core data called "expense" attributes "date, category, amount..........". how can list expenses index list, based on "year-month" date attribute?
from apple's document , tutorials, know how make index list table view. requires provide "array of arrays". that's 1 array sections, , 1 sub-array objects in 1 specific section. data structure not this.
from thinking, can is:
- fetch expenses first, extract cases of "year-month" no duplication, put them array, sections
- then fetch expenses based on every section
but think it's kind of heavy work, can achieve more conveniently? or should change data structure ? thank guys :)
you create request return expenses in given range:
//not tested nsdatecomponents* comps = [[nsdatecomponents alloc] init]; nscalendar* cal = [nscalendar currentcalendar]; nsinteger year,month;//set these [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 > %@ , date < %@",start,end]; nsfetchrequest* request = [[nsfetchrequest alloc] initwithentityname:@"expense"]; [request setpredicate:predicate];
then use nsfetchedresultscontroller
populate table view
Comments
Post a Comment