ios - 如何在 iOS TableView 中每行创建多个点击处理程序?

标签 ios objective-c uitableview uitapgesturerecognizer

我在带有几个可点击区域的表格 View 中有一个企业列表。第一个是在单独的 ViewController 上查看业务详细信息的区域,第二个是给业务打电话,第三个是在 map 上定位业务。

我是 iOS 新手,所以我不确定执行此操作的最佳方法。

在 Android 中,这相当容易。我可以在表适配器的 getView() 方法中执行类似的操作:

linearLayoutBusiness.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(context, DetailsActivity.class);
            i.putExtra("business", business);
            context.startActivity(i);
        }
    });

到目前为止,我在 cellForRowAtIndexPath 方法中拥有的是:

    //business details tap handler
    UITapGestureRecognizer *touchOnBusinessDetails = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(detailsTapHandler)];
    [cell.BusinessInfoView addGestureRecognizer:touchOnBusinessDetails];


    //call view tap handler
    UITapGestureRecognizer *touchOnCallView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(callViewTapHandler)];
    [cell.callView addGestureRecognizer:touchOnCallView];

    //location view tap handler
    UITapGestureRecognizer *touchOnLocationView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(locationViewTapHandler)];
    [cell.locationView addGestureRecognizer:touchOnLocationView];


    return cell;
}

- (void)detailsTapHandler{

    NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
    NSInteger row = selectedIndexPath.row;
    BusinessEntity *businessTapped = businesses[row];

    //open details view
    [self performSegueWithIdentifier:@"detailsSegue" sender:self];
    NSLog(@"detailsView");
}

- (void)callViewTapHandler{
    NSLog(@"callView");
}

- (void)locationViewTapHandler{
    NSLog(@"locationView");
}

我遇到的问题是selectedIndexPath总是为nil,我无法在detailsTapHandler方法中发送与所选行对应的业务对象。

我走在正确的轨道上吗?或者有更好的方法吗?

最佳答案

您的选择器应如下所示:

- (void)locationViewTapHandler:(UIGestureRecognizer*)sender 
{
    NSLog(@"locationView");
}

因此,当选择器被触发时,它会作为参数传递给手势识别器。然后您可以使用 sender.view 属性访问附加到手势识别器的 View 。

了解了 View ,您可以向上爬 View 层次结构以获取包含它的UITableViewCell,然后调用[tableView indexPathForCell:cell],或者只存储模型在 View 的 tag 属性中索引并直接通过索引访问模型。

关于ios - 如何在 iOS TableView 中每行创建多个点击处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27883292/

相关文章:

ios - CGRectApplyAffineTransform 然后 CGAffineTransformInvert 不会返回到原始位置

iphone - TWRequest 中的 EXC_BAD_REQUEST 问题

ios - UITableView 行移动怪异

ios - 提交新版本时可以降级 iOS 部署目标吗?

ios - SKShapeNode .strokeColor 在更改 node.alpha 时看起来与 .fillColor 相同

objective-c - 目标 - C : Combine multiple string arrays

ios - Realm Platform 数据未通过 Storyboard-Swift 4 加载到 TableView 中

ios - 以编程方式在 UIButton 上设置目标/操作?

iphone - 如何在iPhone的字符串中提取单个字节?

ios - 在 UIView 中隐藏非子 UIView