iPhone: Repeating Rows in Each Section of Grouped UITableview
        Posted  
        
            by Rank Beginner
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Rank Beginner
        
        
        
        Published on 2010-03-25T20:04:18Z
        Indexed on 
            2010/03/29
            15:53 UTC
        
        
        Read the original article
        Hit count: 415
        
I'm trying to learn how to use the UITableView in conjunction with a SQLite back end. My issue is that I've gotten the table to populate with the records from the database, however I'm having a problem with the section titles. I am not able to figure out the proper set up for this, and I'm repeating all tasks under each section.
The table looks like this. The groups field is where I'm trying to pull the section title from.
TaskID  groups      TaskName    sched   lastCompleted   nextCompleted   success
1       Household   laundry     3       03/19/2010      03/22/2010      y 
1       Automotive  Change oil  3       03/20/2010      03/23/2010      y 
In my viewDidLoad Method, I create an array from each column in the table like below.
    //Create and initialize arrays from table columns
    //______________________________________________________________________________________
    ids =[[NSMutableArray alloc] init];
    tasks =[[NSMutableArray alloc] init];
    sched =[[NSMutableArray alloc] init];
    lastComplete =[[NSMutableArray alloc] init];
    nextComplete =[[NSMutableArray alloc] init];
    weight =[[NSMutableArray alloc] init];
    success =[[NSMutableArray alloc] init];
    group =[[NSMutableArray alloc] init];
// Bind them to the data
//______________________________________________________________________________________    
    NSString *query = [NSString stringWithFormat:@"SELECT * FROM Tasks ORDER BY nextComplete "];
    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2( database, [query UTF8String],
                           -1, &statement, nil) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            [ids addObject:[NSString stringWithFormat:@"%i",(int*) sqlite3_column_int(statement, 0)]];
            [group addObject:[NSString stringWithFormat:@"%s",(char*) sqlite3_column_text(statement, 1)]];
            [tasks addObject:[NSString stringWithFormat:@"%s",(char*) sqlite3_column_text(statement, 2)]];
            [sched addObject:[NSString stringWithFormat:@"%i",(int*) sqlite3_column_int(statement, 3)]];
            [lastComplete addObject:[NSString stringWithFormat:@"%s",(char*) sqlite3_column_text(statement, 4)]];
            [nextComplete addObject:[NSString stringWithFormat:@"%s",(char*) sqlite3_column_text(statement, 5)]];
            [success addObject:[NSString stringWithFormat:@"%s",(char*) sqlite3_column_text(statement, 6)]];
            [weight addObject:[NSString stringWithFormat:@"%i",(int*) sqlite3_column_int(statement, 7)]];
        }
        sqlite3_finalize(statement);
    }
In the table method:cellForRowAtIndexPath, I create controls on the fly and set their text properties to objects in the array. Below is a sample, I can provide more but am already working on a book here... :)
/create the task label 
NSString *tmpMessage;
tmpMessage = [NSString stringWithFormat:@"%@ every %@ days, for %@ points",[tasks objectAtIndex:indexPath.row],[sched objectAtIndex:indexPath.row],[weight objectAtIndex:indexPath.row]];
    CGRect schedLabelRect = CGRectMake(0, 0, 250, 15);
    UILabel *lblSched = [[UILabel alloc] initWithFrame:schedLabelRect];
    lblSched.textAlignment = UITextAlignmentLeft;
    lblSched.text = tmpMessage;
    lblSched.font = [UIFont boldSystemFontOfSize:10];
    [cell.contentView addSubview: lblSched];
    [lblSched release];
My numberOfSectionsInTableView method looks like this
// Figure out how many sections there are by a distinct count of the groups field
// The groups are entered by user when creating tasks   
//______________________________________________________________________________________    
    NSString *groupquery = [NSString stringWithFormat:@"SELECT COUNT(DISTINCT groups) as Sum FROM Tasks"];
    int sum;
    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2( database, [groupquery UTF8String],
                           -1, &statement, nil) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            sum = sqlite3_column_int(statement, 0);
        }
        sqlite3_finalize(statement);
    }
    if (sum=0) {
        return 1;
    }
    return 2;
}
I know I'm going wrong here but this is all that's in my numberOfRowsInSection method
return [ids count];
© Stack Overflow or respective owner