UITableView: Handle cell selection in a mixed cell table view static and dynamic cells
- by AlexR
I am trying to mix dynamic and static cells in a grouped table view: I would like to get two sections with static cells at the top followed by a section of dynamic cells (please refer to the screenshot below). I have set the table view contents to static cells.
Edit
Based on AppleFreak's advice I have changed my code as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell;
if (indexPath.section <= 1) { // section <= 1 indicates static cells
cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
} else { // section > 1 indicates dynamic cells
CellIdentifier = [NSString stringWithFormat:@"section%idynamic",indexPath.section];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
}
return cell;
}
However, my app crashes with error message
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'UITableView dataSource
must return a cell from tableView:cellForRowAtIndexPath:'
for section 0 and row 0. The cell returned from cell = [super tableView:tableView cellForRowAtIndexPath:indexPath] for section 0 and row 0 is nil.
What is wrong with my code? Could there be any problems with my outlets? I haven't set any outlets because I am subclassing UITableViewController and supposedly do not any outlets for tableview to be set (?). Any suggestions on how to better do it?
Edit II
I have recreated my scene in storyboard (please refer to my updated screen shot above) and rewritten the view controller in order to start from a new base. I have also read the description in Apple's forum as applefreak suggested. However, I run in my first problem with the method numberOfSectionsInTableView:tableView, in which I increment the number of static sections (two) by one.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [super numberOfSectionsInTableView:tableView] + 1 ; }
The app crashed with the error message:
Terminating app due to uncaught exception 'NSRangeException', reason:
'* -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
Why is this code not working for me even though I followed Apple's and applefreak recommendations? It is possible that the tableView has changed a bit in iOS 6?
Solution: I got this to work now using AppleFreaks code sample in his answer below. Thank you, AppleFreak!
Edit III: Cell Selection:
How can I handle cell selection in a mixed (dynamic and static cells) cell table view?
When do I call super and when do I call self tableView?
When I use [[super tableView] selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone] and try to check for the selected index paths with:
UITableView *tableView = [super tableView];
if ( [[tableView indexPathForSelectedRow] isEqual:customGrowthIndexPath] ) { .. }
I get an return value of nil.
As I can't find the source of my error, I really would appreciate your help