Can not select specific UITableViewCell after reloading tableView - ios

My UITableView shows answers for questions.
To switch questions I apply CATransition to tableView and call reloadData method.
In case if user did select one row, went to another question and then returned back to the first question, I want the chosen answer to be selected.
I call
[tableView selectRowAtIndexPath:indexPath
animated:YES
scrollPosition:UITableViewScrollPositionNone];
in delegate method
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
But when answers appear after reloading UITableView, there are no selected cells. If I move cell out of the screen and then move it back, it becomes selected.

you need to implement following UITableView delegate method to perform any action on selection of cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// [self doSomethingWithRowAtIndexPath:indexPath];
// write your action on cell here
}

Related

How to deselect tableview cell when selecting another tableview cell in the meantime?

I an doing a table view cell auto selecting for the first time by using
[cell setSelected:YES animated:YES];
It select the cell programmatically for the first time. It works fine. Now I want to deselect that cell when i select another cell in the meantime. how can i do that. I know when i select a cell then tableview delegate method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
is called and when deselect a cell then "did deselect row at index path" is called.
But the problem is i select the cell programmatically and when i select another cell then delegate method
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
doesn't call.
What should i do to solve this problem or what will be the logic to solve that problem?
Instead of calling setSelected:animated: on the cell directly, call the selectRowAtIndexPath:animated:scrollPosition: method of the table view to set the initial selection.
You can use below method to deselect row:
[tableView deselectRowAtIndexPath:indexPath animated:YES];
You just need to stored index when it's selected. So you just need to check if you have selected indexpath then make it deselect.
Take an object of NSIndexPath:
NSIndexPath *currentSelectedPath;
Update your didSelectRow method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(currentSelectedPath)
{
[tableView deselectRowAtIndexPath:currentSelectedPath animated:YES];
}
currentSelectedPath = indexPath;
// Do your other stuff
}

table view cell restore normal status after actions

I'm using Objective-C. Here I have a tableview, and I set some actions using this method:
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
So when I slide the cell, it looks like this: the cell
I want to dismiss these action buttons after I clicked on one of them. Which method should I use?
These code will do well:
[tableView setEditing:NO animated:YES];

How to find out if a UITableViewCell has scrolled away and is invisible

I have a UITableViewCell that observes (via KVO) some properties. This only makes sense if the cell is visible. I want to remove this observer when the cell scrolls away, and before prepareForReuse is called.
How can I do this?
Your table view delegate can implement
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
and in there you can call a method on the cell to perform the cleanup you want.

UITableView not detecting the the accurate tapped row

I have just added a UITableView in to my storyboard and the suggested constraints to it like that.. Now i have written very simple code with some delegate and datasource methods implemented like this. . Now when i tap the first row it detects nothing and check the console in the below image, when i press the second row it tells me that the indexPath.row is 0.. can some body point out that what i am doing wrong here.
You are calling -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath which is called when you deselect row , instead call
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
It's because you are using didDeSelectRowAtIndexPath: instead of didSelectRowAtIndexPath: method.
You have to call "didSelectRowAtIndexPath" Delegate method.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
This method only return which row we have selected.

IOS UITableViewCell De-select a view once selected and returned to page

How can I deselect a cell when returning to a view?
I have an orange down state which is applied to a cell when selected - the cell navigates to a modal view when clicked - when i click back button the cell is still selected.
I have tried applying this code -
[tableView deselectRowAtIndexPath:indexPath animated:YES];
to my cellForRowAtIndexPath method - but it doesn't do anything!
Update - Having done a bit of research - It appears Ive missed some valuable information out of this question! - my table view is a UITableView embedded in a View Controller - not a UITableViewController - so it sounds like it doesnt have the available methods which are required for the suggestions so far..
You could use UITableViewController's clearsSelectionOnViewWillAppear property.
You should not call deselectRowAtIndexPath in cellForRowAtIndexPath method.
you can do this in viewWillAppear
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear: animated];
NSIndexPath *selectedIndexPath = [tableViewObj indexPathForSelectedRow];
if (selectedIndexPath != nil) {
[tableViewObj deselectRowAtIndexPath:selectedIndexPath animated:YES];
}
}
Or you can write in didSelectRowAtIndexPath as well
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath: indexPath animated:YES];
}
This is the right approach
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath: indexPath animated:NO]; // first line in this method
// rest of code
}

Resources