Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

The easiest way is to style your UITableView to 'UITableViewStyleGrouped'.

UITableView *tab = [[UITableView alloc] initWithFrame:rect style:UITableViewStyleGrouped];

Or you can go to interface builder and in Table View change style from plain to grouped.

The style 'Grouped' divides your table into multiple sections.

The using UITableViewDelegate methods specify all the parameters.

// Tell the number of section in table

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

return numberOfSections; 

}

//Tell the number of rows in each section

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    if (section == 0)
    {
         return 2;
    } else if(section == 1)...

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (indexPath.section == 0 && indexPath.row == 0)
  {
         //Show Amount for Jul 02, 2013
         cell.textLabel.text = @"Jul 02, 2013";
         cell.detailTextLabel = @"20.35";
  }
  // Do the same for all rows and section in table.

}

For further reference - http://mobisoftinfotech.com/iphone-uitableview-tutorial-grouped-table/

You should also definitely check out the Sensible TableView framework. Saves me tons of time when working with Core Data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top