1

Highlight table view cell on long press.

I referred the link

Long press on UITableView

Long press is working fine but table view cell is not highlighting. so I added following line to handleLongPress method

[self.myTableView selectRowAtIndexPath:indexPath animated:NO  scrollPosition:UITableViewScrollPositionNone];

After highlight of long pressed cell, also following conditions should be satisfied,

  1. Touching the same long pressed table view cell should unhighlight which is like a second touch of table view cell.
  2. Touching the other table view cell should highlight like multiple cell selection.
  3. On second touch of other table view cells should unhighlight.

Long press should behave like touching the table view cell but it should not be actual touching functionality. Please guide me.

Community
  • 1
  • 1
user3898700
  • 141
  • 1
  • 12

3 Answers3

0

Try:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
William Hu
  • 15,423
  • 11
  • 100
  • 121
0

please use such type of logic. When you start long press, show row as selected while Long press ended you must deselect row.

-(IBAction)longPressBegan:(UILongPressGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan)
    {
        // Long press detected, Just highlight row 
    }
    else
    {
        if (recognizer.state == UIGestureRecognizerStateCancelled
            || recognizer.state == UIGestureRecognizerStateFailed
            || recognizer.state == UIGestureRecognizerStateEnded)
        {
            // Long press ended, deselect row
        }
    }
}

Hope this will help you out...........

P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Qaiser Abbas
  • 326
  • 3
  • 9
  • If you method fires more than one time, then you are doing right else while adding gesture, you are making some mistake. – Qaiser Abbas Sep 17 '15 at 06:47
  • it fires multiple times to indicate the different states of the gesture (began, changed, ended, etc). So in the handler method. – Qaiser Abbas Sep 17 '15 at 06:48
  • it is firing multiple times and got three states like UIGestureRecogniserStateBegan, UIGestureRecogniserStateChanged and UIGestureRecogniserStateEnded. As soon as i long press, it is highlighting later it is unhighlighting, when touch ends. it is not highlighting like multiple cell selection. – user3898700 Sep 17 '15 at 07:15
0

So I assume all your conditions are only in case when you already long pressed a cell. It looks like you are able to get long press work . so now

  1. Declare a BOOL in your class. BOOL isHiglighted;
  2. in your long press event add

    [self.myTableView selectRowAtIndexPath:indexPath animated:NO      scrollPosition:UITableViewScrollPositionNone];
    
    YourCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
    
    //I dont’t know your model for this tableview so I am using cell tag for it 
    
    //using 1 for selected
    
    //2 for unselected
    
    // 3 for long pressed one 
    
    cell.tag = 3;
    
    isHiglighted = YES;
    
  3. Use following sudo code in your didSelectRowAtIndexPath

    - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    
    {
    
        if(isHiglighted)
        {
            YourCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
            if(cell.tag==3)
            {
                [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
                cell.tag = 2;
                isHiglighted = NO;
            }
            else if(cell.tag==1)
            {
                [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
                cell.tag = 2;
            }
            else 
            {
                [self.myTableView selectRowAtIndexPath:indexPath animated:NO  scrollPosition:UITableViewScrollPositionNone];
                cell.tag = 1;
             }
    
        }
        else
        {
            // do your stuff ofr single tap if user never long pressed
        }
    }
    
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Imran
  • 2,985
  • 19
  • 33
  • didSelectRowAtIndexPath method is not called when cell is unhighlighted, it is called only during cell highlighting. Already tried. – user3898700 Sep 17 '15 at 09:21
  • didSelectRowAtIndexPath is always called unless you implement willselectRowAtIndex path and in that method you return nil or invalid indexpath can you post your code related to this issue not sure where you are facing problem – Imran Sep 17 '15 at 09:31