How to hide UITableViewCellSelection after pressed? - objective-c

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];

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];
}

Strange behavior after selecting cell by code

I ran into some strange behaviour after I selected a cell using this code :
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
CustomCell * cell = (CustomCell*)[_myTableView cellForRowAtIndexPath:indexPath];
[cell setSelected:YES animated:NO];
After this i couldn't select exactly this cell it just didn't respond and
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
was never called.
This wasn't called either:
-(void)tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
What i am doing wrong guys?
You need to call both methods manually because manually cell selection does not call delegate method.
[cell setSelected:YES animated:NO];
[self tableView:_myTableView didSelectRowAtIndexPath:indexPath];
According to Apple's documentation it is expected behavior that the methods you mention are never called after programmatically selecting a cell. More info is in this post.

How to avoid 'double select' on UITableView?

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];
}

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];
}