views:

48

answers:

2

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:

alt text

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?

A: 

First off, the numberOfRows.. method wouldn't appear to me as the proper place to do this kind of logic. I'd rather create a seperate method to determine and return the current game mode (and keep the gamemode updated whenever the user selects another mode). I'd associate integers with gamemodes (1 = "one on one", 2 = "all against all"...), that way in the numberOfRows Method you just need to check "is the current section == 1?" && "which game mode are we in?" to determine how many rows are needed.

If you keep track of the gamemode properly, you don't need to check for equal strings anymore, too.

Toastor
The problem is, I also check if the number of rows in the section is 2, so then I return the actual count of objects, else I return an added object for the `Add New…` row. This is the reason I get the error because as soon as the row is deleted the count is less than 2 so then it returns one more row. Which means there are 2 rows before and after.
Joshua
I'd keep the user interface and the logic behind it seperate. You're looking at the current state of the UI to figure out what state your game is in, and even worse, you're updating it while it is updating, so to speak. You should do it the other way round - for every user action, you update a seperate set of variables representing your gamestate. After that, you update your interface according (only!!) to the updated game state...
Toastor
No, I get that and have created the integer like you said which will change depending on game mode. **My Problem is Hiding the `Add New…` Row when there are 2 Rows in the section**, like I explained in my first comment.
Joshua
A: 

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 my if statement) to make it look un-selectable to the user:

cell.textLabel.textColor = [UIColor lightGrayColor];
cell.editingAccessoryType = UITableViewCellAccessoryNone;

And in the tableView:didSelectRowAtIndexPath: method I left my if statement blank so it wouldn't push another view controller or add a object. Although I did include this code to deselect the row:

[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
Joshua