How can I set a UITableView to default to no selection? indexPath.row = -1 or something similar?
Here's my code for creating the table:
UITableView* tblThisTable = [[UITableView alloc] initWithFrame:CGRectMake(elementX, elementY, elementW, elementH)]; tblThisTable.rowHeight = 30.0; tblThisTable.layer.borderColor = [colorManager setColor:51.0 :51.0 :51.0].CGColor; tblThisTable.layer.borderWidth = 1.0; tblThisTable.delegate = self; tblThisTable.dataSource = self; And my implementation of the delegate protocols:
#pragma mark - Table View Management - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return arrStates.count; } // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.textLabel.text = [arrStates objectAtIndex:indexPath.row]; cell.textLabel.textColor = [colorManager setColor:66.0 :66.0 :66.0]; cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:20.0]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { strSelectedState = [arrStates objectAtIndex:indexPath.row]; } I have a data manager class where I check the values of user entries prior to saving them to a plist. Here is the code relevant to the table (I only have the states table):
...
} else if ([[_arrAllElements objectAtIndex:i] isMemberOfClass:[UITableView class]]) { UITableView* tblThisTable = (UITableView*)[_arrAllElements objectAtIndex:i]; strElementKey = @"State"; int selectedRow = tblThisTable.indexPathForSelectedRow.row; if(selectedRow > -1) { strElementValue = [arrStates objectAtIndex:selectedRow]; } else { strElementValue = @"No Entry"; } ...
So, if a user hits the save button without ever selecting anything in the table I am still getting a 0 value for selectedRow but nothing is showing as selected. I was thinking of UISegementedControls where you can set them to -1 to have no selection. Shouldn't a table do likewise?
strSelectedStatedefined? If a user doesn't select a row it will remain at its default state.didSelectRowAtIndexPath:, but you also mentioned that the bug happens when you save before a user makes any row selection at all ("on my test I don't touch the table but the results are always indexPath.row = 0"). What code receivesindexPath.row = 0?