0

I am creating one app, where i am showing some rss content from url for my UITableView until here its working perfect. But here what i want is when user click and hold on UITableViewCell for more than 5 sec do other functionality. i just want how to know a single selection and click and hold selection for UITableViewCell.

Thanks

1 Answers1

6

You have to add a UILongPressGestureReconizer to your cell, and set it's minimumPressDuration to 5.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
        longPress.minimumPressDuration = 5.0;
        [cell addGestureRecognizer:longPress];

    }

    // Do additional setup

    return cell;
}

EDIT:

Note that if you are using prototypeCells, the !cell condition will never be true, so you would have to write something like:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    if (cell.gestureRecognizers.count == 0) {
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
        longPress.minimumPressDuration = 5.0;
        [cell addGestureRecognizer:longPress];
    }
    return cell;
}
Levi
  • 7,313
  • 2
  • 32
  • 44
  • Thanks Levi, your example is working perfect for me. Can you please tell me how to select content and show copy popup so i can copy the text to clipboard. – mohammed ghousepasha Jan 02 '13 at 13:39
  • In case you are using standard cells, the content is `cell.textLabel.text`. For the popup you should use a popover on iPad or action sheet on iPhone. That automatic popup with the copy message is available only if your text is editable. So you would have to create a custom cell with a `UITextField` or a `UITextView` in it. – Levi Jan 02 '13 at 13:45
  • hi levi, how to know which cell `indexPath` was selected during Tap&Hold. – mohammed ghousepasha Jan 02 '13 at 14:51
  • 1
    Hi. The method's signature is `-(void)handleLongPress:(UILongPressGestureRecognizer)recognizer`, where `recognizer.view` would be your cell. You can call `[self.tableView indexPathForCell:(UITableViewCell *)recognizer.view];` to get the `indexPath` – Levi Jan 02 '13 at 15:45
  • Thanks Levi now every thing working perfect, thanks for your valuable information. – mohammed ghousepasha Jan 03 '13 at 09:45