Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone UITableView Nested Sections

For the acani iPhone app, I'd like display groups (based on interests) in a UITableView. I'd like to organize the groups taxonomically, e.g.:

  • Sports
    • Bat-and-ball
      • Baseball
      • Softball
      • Cricket
    • Hockey
      • Field Hockey
      • Ice Hockey
      • Roller Hockey
  • Engineering
    • Electrical Engineering
    • Biochemical Engineering

How should I arrange this on a UITableView?

I'm thinking that there should be a root UITableView that will have the sections Sports & Engineering, and the cells Bat-and-ball & Hockey will be under the Sports section, and the cells Electrical Engineering & Biochemical Engineering will be under the Engineering section.

Then Bat-and-ball should have its own UITableView, which should have cells Baseball, Softball, and Cricket.

Does this sound like a good way to arrange the UI?

Do you have any sample code or links to Xcode sample code for a UI like this? There's gotta be an Xcode sample project that does something like this. Perhaps the periodic table of elements project or Core Data Books?

Thanks!

Matt

like image 671
ma11hew28 Avatar asked Dec 03 '10 03:12

ma11hew28


People also ask

What is Section in UITableView?

UITableView with sections allows us to separate the list into different categories, so the list looks more organized and readable. We can customize sections as per our need, but in this tutorial, we are covering the basic UITableview with sections. Here's is the video if you prefer video over text. Let Create An App.

How do I add a section in Swift?

To add a section around some cells, start by placing a Section around it, optionally also adding a header and footer. As an example, we could create a row that holds task data for a reminders app, then create a list view that has two sections: one for important tasks and one for less important tasks.

What is IndexPath UITableView?

IndexPath contains information about which row in which section the function is asking about. Base on this numbers you are configuring the cell to display the data for given row.

What is UITableView in Swift?

A view that presents data using rows in a single column.


2 Answers

You got it. A UITableView really isn't designed to show more than two levels of a hierarchy, as sections and rows. If you want to show more than two levels, a "drill-down" approach used in most (all?) iOS apps, where tapping a row presents another UITableView on the navigation stack. (As you say.)

There are lots of Apple sample code projects that use this design pattern.

Edit: just checked and DrillDownSave is a good example, as is SimpleDrillDown.

like image 62
Shaggy Frog Avatar answered Oct 23 '22 18:10

Shaggy Frog


The trick to have nested sections is to have two kinds of rows in the table view. One to represent the second level of sections and another to represent the normal rows in the tableview. Let's say you have a two level array (say sections) to represent the items in your table view.

Then, the total number of sections that we have are just the number of top level sections. The number of rows in each top level section would be the number of subsections + the number of rows in each subsection.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *sectionItems = self.sections[(NSUInteger) section];
    NSUInteger numberOfRows = sectionItems.count; // For second level section headers
    for (NSArray *rowItems  in sectionItems) {
        numberOfRows += rowItems.count; // For actual table rows
    }
    return numberOfRows;
}

Now, all we need to think about is how to create the rows for the table view. Set up two prototypes in the storyboard with different reuse identifiers, one for the section header and another for row item and just instantiate the correct one based on the asked index in the data source method.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *sectionItems = self.sections[(NSUInteger) indexPath.section];
    NSMutableArray *sectionHeaders = self.sectionHeaders[(NSUInteger) indexPath.section];
    NSIndexPath *itemAndSubsectionIndex = [self computeItemAndSubsectionIndexForIndexPath:indexPath];
    NSUInteger subsectionIndex = (NSUInteger) itemAndSubsectionIndex.section;
    NSInteger itemIndex = itemAndSubsectionIndex.row;

    if (itemIndex < 0) {
        // Section header
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SECTION_HEADER_CELL" forIndexPath:indexPath];
        cell.textLabel.text = sectionHeaders[subsectionIndex];
        return cell;
    } else {
        // Row Item
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ROW_CONTENT_CELL" forIndexPath:indexPath];
        cell.textLabel.text = sectionItems[subsectionIndex][itemIndex];
        return cell;
    }
}

- (NSIndexPath *)computeItemAndSubsectionIndexForIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *sectionItems = self.sections[(NSUInteger) indexPath.section];
    NSInteger itemIndex = indexPath.row;
    NSUInteger subsectionIndex = 0;
    for (NSUInteger i = 0; i < sectionItems.count; ++i) {
        // First row for each section item is header
        --itemIndex;
        // Check if the item index is within this subsection's items
        NSArray *subsectionItems = sectionItems[i];
        if (itemIndex < (NSInteger) subsectionItems.count) {
            subsectionIndex = i;
            break;
        } else {
            itemIndex -= subsectionItems.count;
        }
    }
    return [NSIndexPath indexPathForRow:itemIndex inSection:subsectionIndex];
}

Here's a detailed post on how to do this.

like image 4
Sapan Diwakar Avatar answered Oct 23 '22 19:10

Sapan Diwakar