Ios table with dynamic partitions and two different prototype cells

I am new to ios programming, so feel free to if the question is simple. I have a master data table mapped to a table view controller. The data in it currently looks like this: there is one prototype cell: simple table with no sections

I need to summarize the data by dates and display the details of each date in another section with a summary total, like the first row. Sort of:

sectioned table with two prototype cells

My question is, is this doable? I think I need to create sections and two prototype cells in each cell of the table. A quick review would be appreciated.

Thanks everyone!

+4
source share
3 answers

An easy way to do this is to use section headings. You can use a single line ( @"%@: %@", date, total ) or a shell view with a label on the left for the date and 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]; } 

Of course, you will need to implement dateStringForSection: or wrappedHeaderForSection:

+4
source

The easiest way is to set the UITableView to "UITableViewStyleGrouped".

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

Or you can go to the interface builder and in the style of changing the table view from simple to grouped.

The "Grouped" style divides your table into several sections.

Using the UITableViewDelegate methods defines all parameters.

// Provide the section number in the table

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

 return numberOfSections; 

}

// Report 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 links - http://mobisoftinfotech.com/iphone-uitableview-tutorial-grouped-table/

+3
source

You should also definitely check out the Sensible TableView environment. Saves a ton of time when working with Core Data.

+1
source

Source: https://habr.com/ru/post/1490344/


All Articles