Search code examples
iphoneuitableviewfmdb

sections and index in UITableView from Sqlite (FMDB)



I am trying to get the sections and index for UITableView from Sqlite (I am using FMDB as the wrapper).
I just can't seem to figure out where to start.
I can read the information back from the DB and store it in a NSMutableArray.

I have tried the following site but he is using a plist. http://www.icodeblog.com/2010/12/10/implementing-uitableview-sections-from-an-nsarray-of-nsdictionary-objects/

Any advice would be appreciated.


Solution

  • you need to make an array of arrays. the main array will represent the tableview sections while the inner arrays will be the contents of the sections.

    In the numberOfSectionsInTableView put

    return [mainArray count];
    

    In the cellForRowAtIndexPath:

    NSMutableArray *tempDict = [mainArray objectAtIndex:indexPath.section];
    NSArray *tempArray = [tempDict objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@", [tempArray valueForKey:@"name"]];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [tempArray valueForKey:@"detail"]];
    return cell;
    

    p.s.the last object is a NSDictionary in my case, that's why I use valueForKey. Good luck!