TableView delegate method called only after a second cell is selected - iphone

I'm dealing with a problem that is really making me crazy. I've got a table view added to a view using IB. This tableView has a delegate and a datasource. The delegate is the view controller, the data source is another class that packs information for displaying them. The data source works and fill the table with correct data.
I'm not able to say the same for the delegate. The delegate implements the classic - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath and I have an NSLog inside that method that logs the cell selected.
The problem is that if I selected a cell, the cell changes its color in blue(selected state), but it doesn't trigger the delegate methods. If I press another cell now the new selection on a new cell pushes the detailviewcontroller with the right informations.
If I pop the cell is deseleted- since in viewWillAppear I put a method for deselect selected cell-, if I press a cell again nothing happens just blue selection,if I press another one it pushes the detail view.
I tried to make few changes to make the case easy as possible:
.used default styled cells
.added the tableview programmatically
.check the delegate of the tableview in different part of the code
Everything seems right I really can't understand.

Use
(void)tableView:(UITableView *)tableView selectRowAtIndexPath:(NSIndexPath *)indexPath
instead of
(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

Related

How do I make a table view cell work as a button?

I am trying to make an app that the user will press a cell and the cell will function as a button, now how do I make the cell function as a button and do an action or how do I know which cell is being pressed?
Please read the document but basically when you set the delegate of the tableviewcontroller it calls this method when clicked:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
The indexpath holds the section and the row. If you are using only one section then indexPath.row will return the index of the cell that was clicked.
You implement the UITableViewDelegate method tableView:didSelectRowAtIndexPath:.
That method will be called whenever the user taps any of the cells. If the passed indexPath is the index path of your button cell, then you do whatever should happen.
Read Apple's documentation on UITableViewDelegate
And you will find tableView:didDeselectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
this method gets called when the user taps a cell. indexPath.section and indexPath.row are your cell section and row indexs.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html
If I understood your question correctly, you simply want to run some code when a user touches a cell.
The UITableViewDelegate has a method called tableView:didSelectRowAtIndexPath: (UITableViewDelegate tableView:didSelectRowAtIndexPath:)
Just implement that and you can do what you want in there.

app with tab bar and navigation bars

Hi all,
this is a typical layout of one of apps. Now what I want on clicking of row of one of the view controllers(table view), I want to change the index of of the tab. Lets say I am in 3rd view, I want to move to the 1st view.
I tried with navigation controller's didShowViewController delegate method. Seems its not working. Could you please suggest any other way to do this?
Thanks.
Just use the below code in didSelectRowAtIndexPath method of UITableView, then you just need to set the SelectedIndex for Tabbar.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
[self.tabBarController setSelectedIndex:indexPath.row];
}

Weird problem with iPhone UITableView cell not un-highlighting after clicking

I have an app I am working on, that has a table view with a few cells, and when you hit a cell, the nav controller pushes a new view. Pretty simple stuff. The problem is that when I hit on a row, the whole row highlights blue, and then remains blue all while the view is being presented by the controller, and then if I hit back to go back to the table view, the row is just still completely highlighted Even though I'm not pressing it or anything.
I have made a few apps with the table view before, and have never seen this behavior before, so I am at a loss as to what is going on. Is there some setting that I have set wrong somewhere?
Thanks
You need to deselect cell manually (probably in tableView:didSelectRowAtIndexPath: method):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Your code
...
}
If your view controller is a subclass of UITableViewController, it will automatically deselect the row after you hit the back button. If you don't subclass UITableViewController, you need to call deselectRowAtIndexPath:animated: when you want it deselected.

setting target for disclosure button on subclassed UITableViewCell

I've created a subclass of UITableViewCell to create some custom appearance and UI functionality ('swipe to reveal delete button').
Some of the cells are disclosure/detail disclosure type, and have a UIButtonTypeDetailDisclosure etc added manually. I cannot use the table view controllers' UITableViewCellAccessory for row at index path method.
The question is, how do I set the target of these manually added buttons so that they correctly send the didSelectRowAtIndexPath to their table view controller?
There is no direct way of setting an action the accessory disclosure button. Though you can do this with delegate method
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
See if that works.

How can I call the parent TableView to reload

In this iphone app I'm trying to make it so that when you select a table cell it saves that action, pops the current view controller and calls the previous view controller (which is and always will be a TableView) to reload it's cells with new data.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//update data code here
[self.navigationController popViewControllerAnimated:TRUE];
//Here is where I want the code to reload the view [parentTableView reload]; or something }
That's what I have at the moment when you select a cell in the current Table View
You need need to send -reloadData to the UITableView:
[tableView reloadData];
Edit : So just to clarify, you have a parent UITableViewController which pushes a child UITableViewController onto the navigationController? The code in your sample is implemented in the didSelectRowAtIndexPath in the child view controller?
If this is the case, you need to to add a property to your child UITableViewController which stores a pointer to its parent. Make sure this isn't a reference counted property because that will result in a circular reference. So before you pop your child UITableViewController it can send -reloadData to the tableView property of its parent.