alex440
alex440

Reputation: 1657

How to make auto deselecting tableview cell

I have a table view

and when I click the cell everything is fine

however, when I remove my fingers from the phone, the cell stays selected

how can I make it become deselected when the user stops touching the given cell and does not touch another cell

Upvotes: 0

Views: 786

Answers (6)

Jonathan
Jonathan

Reputation: 2383

In tableView:didSelectRowAtIndexPath:, set the selected property of the cell to NO using the method setSelected:animated:.

E.g.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setSelected:NO animated:YES];
}

NOTE:

Setting the cell's selected property = NO and animated = YES with the above method will cause the separator to disappear. The only way I've found to circumvent this is to set animated = NO but, if I find another way I will update my answer.

Upvotes: 0

Josip B.
Josip B.

Reputation: 2464

Have you assigned delegate to UITableView?

self.tableView.delegate = self;

That is the reason why you experience this

this does not work - the cell does not show indication of being selected

Upvotes: 0

codingPanda
codingPanda

Reputation: 255

Have you considered to handle the touch events?

For example: Add a UITapGestureRecognizer, detect which cell is being touched by using the -(CGPoint)locationInView:(UIView *)view method. Mark the cell as selected when the touch begins and mark as deselected when the touch ends.

Upvotes: 0

Alex Blundell
Alex Blundell

Reputation: 2104

Put the following in the UITableView delegate.

   -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell setSelected:NO animated:YES];
    }

Upvotes: 0

Kumar KL
Kumar KL

Reputation: 15335

Simple :

In this method : didSelectRowAtIndexPath

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIActionSheet *photoPicker ;

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

  }

Upvotes: 1

geo
geo

Reputation: 1791

you have to set the style in cellForRowAtIndexPath or change the cell in willSelectRowAtIndexPath and change back in didSelectRowAtIndexPath. For example

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // ...
    cell.selectionStyle = UITableViewCellSelectionStyleBlue; // or an other style
    // ...
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // ...
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    // ...
}

Upvotes: 0

Related Questions