在tableView中对单元格进行长时间触摸(触摸和等待)

时间:2014-08-25 16:11:45

标签: ios objective-c xcode

我有一个tableview,我希望当我触摸要导航到editViewController的单元格时,以及当我长时间触摸(触摸并等待)单元格以导航到DetailsViewController

1 个答案:

答案 0 :(得分:0)

1)向您的小区添加UILongPressGestureRecognizer

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        //add longPressGestureRecognizer to your cell
        UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                              initWithTarget:self action:@selector(handleLongPress:)];
        //how long the press is for in seconds
        lpgr.minimumPressDuration = 1.0; //seconds
        [cell addGestureRecognizer:lpgr];

    }


    return cell;
}

2)处理longPress并推送给你editViewController

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{

    CGPoint p = [gestureRecognizer locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
    if (indexPath == nil) {
        NSLog(@"long press on table view but not on a row");
    }
    else
    {
        if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
        {
            NSLog(@"long press on table view at row %ld", (long)indexPath.row);

            editViewController *editView = [self.storyboard instantiateViewControllerWithIdentifier:@"editView"]; //dont forget to set storyboard ID of you editViewController in storyboard
            [self.navigationController pushViewController:editView animated:YES];
        }
    }

}

3)正常按下您的DetailsViewController

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailsViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:@"detailView"]; //dont forget to set storyboard ID of you editViewController in storyboard
    [self.navigationController pushViewController:detailView animated:YES];
}
相关问题