Modal UITableView Won't recognize first click - ios

I have a UITableViewController that I display modally. When the user clicks on any cell, I need to capture that choice and dismiss the view controller. The weird thing is that the first cell that is clicked is not recognized. When the user clicks on another cell, that is recognized and everything works as intended. Obviously, I need the first click to be the one that is recognized. What am i doing wrong?
- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"cell clicked");
// capture user selection and return to previous screen
[self dismissViewControllerAnimated:YES completion:nil];
}

You are using tableView:didDeselectRowAtIndexPath: instead of tableview:didSelectRowAtIndexPath: ;)

Related

Segue only performed upon selecting another table row

I am currently writing an app that downloads the RSS feed of a wordpress website and displays it in a table view. When you tap on an item in the list, it goes to the details view and displays the content. However, I have a strange issue with the segue to get from the Table View to the Detail View whereby the segue is not performed immediately after tapping on a row, but only when another row is tapped afterwards. The content displayed is that of the first selection.
For example, if i click row 1, it is highlighted but there is no transition. Nothing happens until I click another row, at which point the segue is performed to the detail view which shows the information for row 1. I have the following methods to perform the segue:
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selectedFeedItem = _feedItems[indexPath.row];
[self performSegueWithIdentifier:#"detailSegue" sender:self];
}
and
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DetailViewController *detailVC = segue.destinationViewController;
detailVC.selectedFeedItem = _selectedFeedItem;
}
I am very new to objective-C and iPhone app development so apologies if this is an obvious issue. Any help appreciated. Thanks.
You have...
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
(deselect)
You need...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
(select)

tableView:didSelectRowAtIndexPath / popViewControllerAnimated do not work

When I use
tableView:didSelectRowAtIndexPath / popViewControllerAnimated
it does not work. I push the view controller by another view but when I select a row, it highlights in gray instead of immediately popping to the original view controller. But when I tap another row, it selects the first row I tapped and then goes back to the original view controller.
Here is the code:
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSArray *allTypes = [[ItemStore sharedStore] allTypes];
NSManagedObject *raum = allTypes[indexPath.row];
self.an.leben = raum;
[self.navigationController popViewControllerAnimated:YES];
}
Any ideas?
Replacde didDeselectRowAtIndexPath with didSelectRowAtIndexPath.
The delegate method you have written is didDeselectRowAtIndexPath instead of didSelectRowAtIndexPath.
According to apple, tableView:didDeselectRowAtIndexPath:
Tells the delegate that the specified row is now deselected.
and
tableView:didSelectRowAtIndexPath:
Tells the delegate that the specified row is now selected.
So, what is happening is, when you click on a row, it becomes selected and turns gray and then when you click on the same or any other row, the previous one gets deselected because only one row is selected at once and hence calls this method to open the next view controller with the previous tapped row.
Change tableView:didDeselectRowAtIndexPath into tableView:didSelectRowAtIndexPath.

deselectRowAtIndexPath issue

I have just started working with tableViews, and I have 5 cells in a tableview in a viewController embedded in a navigationController. When a cell is pressed another view is pushed in. My problem is that the cell I press is selected but the selection doesn't go away. I have tried with this code:
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
NewViewController *newViewController = [[NewViewController alloc] init];
newViewController.theTitle = [self.tableView cellForRowAtIndexPath:indexPath].textLabel.text;
[self.navigationController pushViewController: newViewController animated:YES];
}
The tableView is initialized in the viewDidLoad method. Still when I click a cell the new viewController is pushed in but the cell stays selected. What might be the issue here?
You've put your code in the didDeselectRowAtIndexPath - put it in the didSelectRowAtIndexPath. This is an easy mistake to make since didDeselect comes up in code completion before didSelect.
Summary :
You have tableview that is in a ViewController that is in a NavigationController
Use touch a cell
A new ViewController is push on the NavigationController Stack
User hit back and goes back to the screen where you have a tableView
That is what I understand from your question.
At this moment the cell in your tableView should still be selected in order for the user to have a reminder of the last action he as done, but you also want it to get unselected animated at the point ( the mail application is a good example of this behaviour).
So the best place to deselect the cell in your tableView is in - (void)viewDidAppear:(BOOL)animated method of the UIViewController that have the tableview inside it's view. By doing so you will let the user a chance to see the selection before it gently fade away like in mail.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:animated];
}
You can pass nil as argument of deselectRowAtIndexPath, tableView handles it gracefully.
Did you also implement tableView:didSelectRowAtIndexPath:? This method is called every time you tap a cell. So you should put your code there.
The method you are using, tableView:didDeselectRowAtIndexPath: is called whenever a cell is selected and you are tapping a different cell.
I think your problem is that you use wrong method. Change didDeselectRowAtIndexPath to didSelectRowAtIndexPath.

Have to press detail disclosure button twice for data to be passed using prepareForSegue method

EDIT: I have fixed this problem. The code is corrected to show this fix.
I have an app that uses a UITableViewController that displays a list of all 50 states separated into sections alphabetically. I have added the detail disclosure button to the prototype cell in the storyboard and the table displays all the correct information.
When I press the button on a cell, it should segue to a new viewController and display the name of the state as the view title and then display a picture of the license plate in the view.
I am using the -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath method to control the button press and then I am using the -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender to control the values being passed to the new viewController.
The problem I am having is that when I press the detail disclosure button nothing gets passed. NSLogging the values in the new viewController outputs (null), (null) and the view has no title or picture. However, when I press back and then press the same cell's detail disclosure button a second time, it works like it should. The title is correct and the picture displays properly.
The same is true when I choose another cell, the viewController displays the last information sent and I have to press back and then choose the cell again for it to update. When choosing a different cell for the first time, (null, (null) is what the NSlog is outputting. So I believe what is happening is that the first time a detail disclosure button is pressed, no values are passed, but the second time it is pressed, the values are being passed.
Here is the relevant code:
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
//Don't even use this method now.
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UITableViewCell *cell = (UITableViewCell*)sender; //Added this line
self.path = [self.tableview indexPathForCell:cell]; //Added this line
if ([segue.identifier isEqualToString:#"stateSegue"])
{
self.controller = segue.destinationViewController;
self.controller.stateName = [self.stateArray[indexPath.section][indexPath.row]stateName];
self.controller.licensePlateURL = [self.stateArray[indexPath.section][indexPath.row]licensePlateURL];
}
}
Any help with this would be greatly appreciated.
Check which method fires first. Maybe you set properties after segue is performed.
There's no informations about how (when) do you perform segue.
You could add this line to your accessoryButton method:
[self performSegueWithIdentifier:yourIdentifierFromStoryboard sender:self];

Dismiss popover from the button on popup

I have created a popover, containing a table
when user will select the row at that time I want to dismiss popover.
any Idea.
Have a look in the apple documentation... Have a look at the popoverclass...
When your table delegate method gets called, close the popover:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[popoverController dismissPopoverAnimated:YES];
// Handle the row click
}
Where popoverController is an instance variable set up when you created the popover.

Resources