i trying follow post http://www.appcoda.com/ios-programming-index-list-uitableview/ create sections using uitableviewcontroller.
i using 2 nsmutabledictionaries first(_curateditemdictionary) dictionary store key value pairs parse , second (_sectionsdictionary) store these in dictionary key , values category type.
[_sectionsdictionary setobject:[obj objectforkey:@"category"] forkey:_curateditemdictionary]; i did because couldn't store key category , value dictionary dictionaries not support duplicate keys.
i getting numberofsectionsintableview using below code
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview { // return number of sections. for(nsstring *key in [_sectionsdictionary allkeys]) { [_cleanarray addobject:[_sectionsdictionary objectforkey:key]]; } nscountedset *set = [[nscountedset alloc] initwitharray:_cleanarray]; (id item in set) { nsinteger count = (unsigned long)[set countforobject:item]; [_rowforindexarray addobject:[nsnumber numberwithinteger:count]]; } _cleansectionarray = [[nsset setwitharray:_cleanarray] allobjects]; return [_cleansectionarray count]; } how numberofrowsinsection above information ? how know items being iterated particular section ?
here code using create curateditemdictionary parse.com data
pfquery *query = [pfquery querywithclassname:[nsstring stringwithstring:self.classname]]; [query findobjectsinbackgroundwithblock:^(nsarray *objects, nserror *error) { if (!error) { // find succeeded. (pfobject *obj in objects) { self.curateditemdictionary = [[nsmutabledictionary alloc]init]; [_curateditemdictionary setvalue:[obj objectforkey:@"itemname"] forkey:@"itemname"]; [_curateditemdictionary setvalue:[obj objectforkey:@"price"] forkey:@"price"]; [_sectionsdictionary setobject:[obj objectforkey:@"category"] forkey:_curateditemdictionary]; [self.tableview reloaddata]; } else { // log details of failure nslog(@"error: %@ %@", error, [error userinfo]); } }];
based on code provided in comment original post similar that, alternative:
self.sectionsdictionary = [nsmutabledictionary new]; nsmutablearray *sectionsarray; (pfobject *obj in objects) { sectionsarray = [self.sectionsdictionary objectforkey: [obj objectforkey:@"category"]]; if (nil == sectionsarray) sectionsarray = [nsmutablearray new]; [sectionsarray addobject: obj] [self.sectionsdictionary setobject: sectionsarray forkey: [obj objectforkey:@"category"]]; } then in numberofsectionsintableview can amount of sections accessing count method on nsmutabledictionary
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return [self.sectionsdictionary count]; } and if need number of rows in each particular section:
- (nsinteger) tableview:(uitableview*)tableview numberofrowsinsection:(nsinteger)section { nsarray *keys = [self.sectionsdictionary allkeys]; id akey = [keys objectatindex:section]; nsmutablearray *arr = [self.sectionsdictionary objectforkey:akey]; return (arr) ? arr.count : 0; } but of course can loop through keys in sectionsdictionary when preparing datasource , put section index key, not need use allkeys trick.
Comments
Post a Comment