How to avoid 'double select' on UITableView? - objective-c

Here is the screen cap...of a tableView, the two blue cell, is selected.
How I simulated:
I select one tableCell, then, it pushes to the second view, and I back to the first view, and select the second tableCell, can it causes this issue. How can I resolve the problems? Thanks...

Before going to the second view, deselect the cell:
[myTableView deselectRowAtIndexPath:indexPath animated:YES];

Look at this :
- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
This will do the trick!

In tableView:didSelectRowAtIndexPath: , call deselectRowAtIndexPath:animated:

You can try this if the UITableView in a UIViewController.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
}

Related

iOS set selected background to table row after reload data?

I have following issue: I have a UITableView.If you click on a cell, a button is added on cell. No Problem till now... but: After adding button i want the TableView (after its reload) to select the just edited (or added) TableCell.
I've tried this in cellForRowAtIndexPath method
if (bool) {
[cell setSelected:YES animated:YES];
}
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor colorWithRed:34/255.0 green:139/255.0 blue:34/255.0 alpha:1.0]];
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];
return cell;
But it didn't work.
Any suggestion or sample code would be appreciated. Thanks .
For the standard selection you use the following method, described at UITableView reference docs
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
Your design might be improved if you use the custom cell storing information of selection count, that is much better than reloading the table and adding a lot of cell logic outside of the cell.
Why are you using
[tableView reloadData];
You can access the selected cell properties and controls without reloading the data using:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
Here is some code that can help you:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.myButton setHidden:false];
}

UITable view selection stay highlighted

I am using XCode4.2 to develop a storyboard application
I am switching views based on a click in a UITableView after clicking the row is highlighted in blue and the new view is loaded .
when I come back to the original view the row is still highlighted in blue ... how can I disable that ?
I do this in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath];
}
That way I have the index path that was selected every time.
use deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated function to deselect rows in uitableview
call it in your viewWillAppear function
[yourTableView deselectRowAtIndexPath:selectedIndexPath animated:NO];
if you are not sure which is index you select last time you can run a loop on all the index path in your table.
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
for(int i = 0; i <= [yourArr count]; i++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[yourTableView deselectRowAtIndexPath:indexPath animated:NO];
}
}
Note: As I don't know your exact code....you need to modify above code to your needs.
A UITableViewController does this automatically. Otherwise, in your viewDidAppear for the owning controller, you need to call - (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated on the table view.

How to hide UITableViewCellSelection after pressed?

I have a UITableView that in my cellForRowsAtIndexPath, I have this:
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
However, after the action occurs in my UINavigationController and I go back, it's still selected. Also, I have a UIAlertView that pops up for one of my actions. How can I hide it after the user has finished tapping it.
Thanks
Override the delegate method like so:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
You can do this:
[yourTableView deselectRowAtIndexPath:[yourTableView indexPathForSelectedRow] animated:NO];

How to unselect a UITableViewCell after you have done some action using didSelectRowAtIndexPath?

Currently I do some work inside the didSelectRowAtIndexPath delegate and noticed that my row is still "selected" after I come back to the view controller later.
How can I unselect this row inside the delegate itself?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//do logic and ... it remains selected in the view?
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
Typically, you want to deselect the row when the view controller appears again:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}
That's what UITableViewController does implicitly, and animating the deselection process after the view controller appears again is a helpful hint to the user as to which cell they selected previously.

UITableViewCell swiped but delete button doesn't appear

This is odd. I'm right swiping a UITableViewCell in the iPad simulator. Even though the event below fires and the swipedCell is not nil, the Delete button doesn't appear. Actually, it appears-but only sometimes. I never get a bad access or a sigbart.
Here's the code:
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer
{
if (userListSwipeRightRecognizer.state == UIGestureRecognizerStateEnded) {
CGPoint swipeLocation = [userListSwipeRightRecognizer locationInView:self.outletView];
NSIndexPath *swipedIndexPath = [self.outletView indexPathForRowAtPoint:swipeLocation];
UITableViewCell* swipedCell = [self.outletView cellForRowAtIndexPath:swipedIndexPath];
[swipedCell setEditing:YES];
}
}
Is this just a simulator issue or am I doing something wrong?
If you simply want to enable swipe-to-delete on your table, there is a much easier way to do it. Implement tableView:commitEditingStyle:forRowAtIndexPath: in your data source and the table view will automatically show the delete button when a cell is swiped.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
If you defined your UITableView in your header, then try:
swipedCell = [self.outletView cellForRowAtIndexPath:swipedIndexPath];
If you use a custom cell and override setEditing, you must call the super method or your delete controls will not be drawn.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
}