Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

ios - uitableview: nested section headers

Im trying to implement a uitableview with the following structure:

  • section group 0
    • section 0
      • cell 0
      • cell 1
      • cell 2
    • section 1
      • cell 0
      • cell 1
      • cell 2
  • section group 1
    • section 0
      • cell 0
      • cell 1
      • cell 2
    • section 1
      • cell 0
      • cell 1
      • cell 2

and it should scoll like this screenshot (1-2-3-4): http://dl.dropbox.com/u/2213241/uitableview.png

So always two sections are visible.

How do i implement this? Or has anyone implemented this already?

Thanks :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...