4

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?

4
  • How is strSelectedState defined? If a user doesn't select a row it will remain at its default state. Commented Apr 16, 2013 at 13:36
  • strSelectedState is defined as nil but I am not checking that. I have a data manager class where I pass all the objects I need when the user saves, see additional edit above to see how I am checking the table values Commented Apr 16, 2013 at 13:41
  • You are checking 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 receives indexPath.row = 0? Commented Apr 16, 2013 at 13:43
  • I posted it in the OP Commented Apr 16, 2013 at 13:45

5 Answers 5

1

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"; } 
Sign up to request clarification or add additional context in comments.

3 Comments

I'm not saving anything right now - on view did load I am building the table, I have a save button, when clicked I gather all relevant data - on my test I don't touch the table but the results are always indexPath.row = 0. The table doesn't show a row selection (where it is highlighted in blue). See my edits to the OP.
Can you post the code where you are gathering those results? I'm not sure if you're updating an ivar in tableView:didSelectRowAtIndexPath: or if you're asking the table view for indexPathForSelectedRow. If the latter, it is probably returning a nil path and indexPath.row will equal 0 in that case.
Edited the OP - I'm using didSelectRowAtIndexPath
0

Put this code in your .m file and it will deselect selected row.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; } 

1 Comment

I should of been more clear - can I do this when I init the table? I have a table of US states and when I go to save the user entries it is defaulting to the first row item (Alabama) when I haven't selected anything. They don't have to pick a state but it can't default to one either.
0

The solution below will work. Edited your code for creating the cell:

// 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.selectionStyle=UITableViewCellSelectionStyleNone; } 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; } 

I added a line:

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

More information can be found here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html

1 Comment

I don't think he means the cell selection state, he means selection as in "a user's choice".
0

If you want to set a all UITableView to default to no selection (scrolling also not touchable)

then use property of table view which is -

tableView.userInteractionEnabled=NO; 

And if you want to stop selection of cell then,

cell.selectionStyle=UITableViewCellSelectionStyleNone; 

1 Comment

Don't want to do either, I just want it to default to no selection on load.
0

In Swift

// IF tableview selected row let selectedIndexPath = tableView_Main.indexPathForSelectedRow if selectedIndexPath != nil { tableView_Main.deselectRowAtIndexPath(selectedIndexPath!, animated: true) } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.