Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios table with dynamic sections and two different prototype cells

I am new to ios programming so bear with me if the question is simple. I have a core data table mapped to a table view controller. The data in it currently looks as follows - there is one prototype cell: simple table with no sections

I need to sum up the data by dates and show the details of each date in a different section with the summed up total coming up as the first row. Something like:

sectioned table with two prototype cells

My question is is this doable? I am thinking I need to create sections and two prototype cells within each table cell. Would appreciate quick feedback.

Thanks all!

like image 514
serverman Avatar asked Feb 17 '23 00:02

serverman


1 Answers

The easy way to do this is using section headers. You can either use a single string (@"%@: %@", date, total) or a wrapper view with a label on the left for the date and on the right for the total.

-(NSString *) tableView:(UITableView *)tv titleForHeaderInSection:(NSInteger)s 
{
    NSString *dateString = [self dateStringForSection:s];
    float total = [self totalForSection:s];
    return [NSString stringWithFormat:@"%@: %0.2f", dateString, total];
}

Or

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [self wrappedHeaderForSection:s];
}

You'll have to implement dateStringForSection: or wrappedHeaderForSection: appropriately, of course.

like image 72
Kevin Avatar answered May 03 '23 23:05

Kevin