mohammed ghousepasha
mohammed ghousepasha

Reputation: 35

how to recognize UITableView Cell Touchup inside for more than 5 sec from User

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

Upvotes: 0

Views: 1609

Answers (1)

Levi
Levi

Reputation: 7343

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

Upvotes: 6

Related Questions