I would do some structure like an array of sections, and every section should have a title and an array of rows. In JSON style, something like:
[
{
"section_title": "My section 1",
"section_rows": [
{"title": "Lecture 1"},
{"title": "Lecture 2"}
]
},
{
"section_title": "My section 2",
"section_rows": [
{"title": "Lecture 3"},
{"title": "Lecture 4"}
]
}
]
With that, your methods would be something like:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return myArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
__weak NSDictionary *section = myArray[section];
return [section[@"section_rows"] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
__weak NSDictionary *lecture = myArray[indexPath.section][@"section_rows"][indexPath.row];
// Configure your cell here
}
// This method should probably be replaced with this one from UITableViewDelegate:
// - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
__weak NSDictionary *sectionInfo = myArray[section];
return sectionInfo[@"title"];
}
Instead of making a mess trying to count/access your data, you should pre-format it to an easy to handle structure before sending/showing it on your UIViewController
. Don't just get your UIViewController
dirty because of data, it should be the other way around and your view should be passive.
I hope this is what you're asking for, I'm not quite sure what you mean by "expandable" table view.