PruitIgoe PruitIgoe - 3 months ago 16
iOS Question

UITableView set it to no selection

How can I set a

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?


How are you saving the row? If you are saving an un-initialized NSInteger/int that might explain why it's defaulting to 0.

When you ask the table for indexPathForSelectedRow, you need to check if it is nil before grabbing the row property (the row of a nil path will always be 0). This updated code should do what you need:

NSIndexPath *selectedPath = tblThisTable.indexPathForSelectedRow;
if (selectedPath != nil) { 
    strElementValue = [arrStates objectAtIndex:selectedPath.row];
} else {
    strElementValue = @"No Entry";