1

I'm new at developing with XCode and Objective-C and I hope you can help me.

The problem is, I have an UITableViewController with an UITableView (created with the InterfaceBuilder).

The cells under the section headers are expandable.

Now I want to dynamically create multiple UITableViews under the existing TableView.

The style will be the same like the existing TableView's style.

Could you tell me how it is possible to create these TableViews programmatically?

Thank you very much

Michael

Charles
  • 50,943
  • 13
  • 104
  • 142
Michael
  • 105
  • 1
  • 3
  • 9
  • Have you looked into doing a grouped table view? – DROP TABLE users Aug 15 '12 at 14:37
  • 2
    Why make multiple tableviews when you can easily make different sections in a single one? – Totumus Maximus Aug 15 '12 at 14:37
  • @DROPtableusers: No I didn't, but thanks I'm going to do now. Is it possible to make sections in each group? – Michael Aug 15 '12 at 14:45
  • @TotumusMaximus: Because I also need the sections. The new UITableViews should categorize my data at a higher level. – Michael Aug 15 '12 at 14:48
  • @Michael hope it fits your needs, I think this might be what you are looking for. – DROP TABLE users Aug 15 '12 at 14:49
  • @DROPtableusers Ok I see, the groups in the grouped UITableView are also only sections. Is It possible to make sections within the groups (which are also sections)? – Michael Aug 15 '12 at 14:53
  • Welcome to Stack Overflow! Please be aware that tags are not keywords. That is, you don't need to stuff the tag list full of the same words you use in your question title. – Charles Aug 15 '12 at 17:29

1 Answers1

0

From what you are saying try using a grouped table view. Check out this link for a quick overview, and go to the grouped table view section.

Edit found this example here:

Seems like it is what you are looking for. And a very cool idea also.

You'll have to just make your own custom header row and just put that as the first row of each section. Subclassing the UITableView or the headers that are on there now would probably be a huge pain and I'm not sure you can easily get actions out of them the way they work now. You could easily set up a cell to LOOK like a header, and setup the tableView:didSelectRowAtIndexPath to expand or collapse the section it is within manually.

If I were you I'd store an array of booleans corresponding the the "expended" value of each of your sections. You could then have the tableView:didSelectRowAtIndexPath on each of your custom header rows toggle this value and then reload that specific section.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        ///it's the first row of any section so it would be your custom section header

        ///put in your code to toggle your boolean value here
        mybooleans[indexPath.section] = !mybooleans[indexPath.section];

        ///reload this section
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
    }
}

You'd then setup your number numberOfRowsInSection to check the mybooleans value and return either 1 if the section isn't expanded, or 1+ the number of items in the section if it is expanded.

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

    if (mybooleans[section]) {
        ///we want the number of people plus the header cell
        return [self numberOfPeopleInGroup:section] + 1;
    } else {
        ///we just want the header cell
        return 1;
    }
}

You would also have to update your cellForRowAtIndexPath to return a custom header cell for the first row in any section.

Community
  • 1
  • 1
DROP TABLE users
  • 1,955
  • 14
  • 26
  • @Michael Could you not just use the different groups as the sections? (maybe I don't understand what you are trying to do exactly?) – DROP TABLE users Aug 15 '12 at 14:59
  • Ok here a quick draft of what I would need: http://desmond.imageshack.us/Himg36/scaled.php?server=36&filename=owno.png&res=landing – Michael Aug 15 '12 at 15:08
  • Ok I only had to set the text of the section header and i got all i want :D Thank you very much for your tip using the grouped style :) – Michael Aug 15 '12 at 15:23
  • Ah no sorry: I want to have multiple sections per group, so what I would need are sections within sections. Please tell me: is that possible? Thanks – Michael Aug 15 '12 at 15:25
  • @Michael check my edit, I think it might be what you are looking for. I've never tried this before, but I think it should work well for you. – DROP TABLE users Aug 15 '12 at 18:09
  • @Michael there is also a very good example [at this page on the apple developer site](http://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010139-Intro-DontLinkElementID_2) – DROP TABLE users Aug 15 '12 at 19:41