In my ‘Sectioned’ UITableView I have two sections, the first one for Attributes like name and the second one is editable where you can add objects.
Here’s a picture of it:
There’s an attribute that the user can change (in this case Type) which would to change the number of rows in second section. To be more specific, if one property is selected the maximum number of rows is 2 and then there would be no Add New… row.
Here’s what I’ve tried (in my UITableViewController) …
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([self isToManyRelationshipSection:section]) {
NSArray *sectionKeys = [rowKeys objectAtIndex:section];
NSString *row0Key = [sectionKeys objectAtIndex:0];
if ([[NSString stringWithFormat:@"%@", [managedObject valueForKey:@"type"]]
isEqualToString:[NSString stringWithFormat:@"One on One"]] && [[managedObject valueForKey:row0Key] count] == 2){
return [[managedObject valueForKey:row0Key] count];
}
return [[managedObject valueForKey:row0Key] count] +1;
}
return [rowLabels countOfNestedArray:section];
}
But if the user tries to remove a row when there are already 2 rows the app crashes because there would be the same number of rows before and after the deletion.
How do I get around this and do this properly?
I ended up doing this differently to what I first wanted to do.
My reasoning was that my current implementation gave me an error in the debugger which in turn crashed the app. This error seemed un-avoidable so I decided to simply disable the cell if there are two rows, instead of hiding it.
To do this in my
tableView:cellForRowAtIndexPath:method I added the following code (in myifstatement) to make it look un-selectable to the user:And in the
tableView:didSelectRowAtIndexPath:method I left myifstatement blank so it wouldn’t push another view controller or add a object. Although I did include this code to deselect the row: