如何使用按钮操作(带参数)从自定义 UITableViewCell 推送视图 [英] How to push a view from custom UITableViewCell with button action (with parameters)

查看:24
本文介绍了如何使用按钮操作(带参数)从自定义 UITableViewCell 推送视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义单元格上添加了一个按钮.它推动了一个观点.我为 CustomTableViewCell.h 上的按钮创建了属性

I added a button on the custom cell. It pushes a view. I created property for button on CustomTableViewCell.h

@property (weak, nonatomic) IBOutlet UIButton *selectedMapButton;

我想使用 selectedMapButton 在 TableViewController.m 上推送视图我试过这段代码,但有些不对劲.

I want to use selectedMapButton for push a view on TableViewController.m I tried this code, but something is not right.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cord = cellSight[@"S_Coodinates"];

    [cell.selectedMapButton addTarget:self action:@selector(sightMapButton:cord) forControlEvents:UIControlEventTouchUpInside];

    //When I call sightMapButton method. It gives an error

    return cell;
}

- (void)sightMapButton: (NSString *)withCoordinates
{
    TOCGSightseeingMapKitViewController *mapKitController = [[TOCGSightseeingMapKitViewController alloc] init];
    mapKitController.sightCoordinates = withCoordinates;

    [self.navigationController pushViewController:mapKitController animated:YES];
}

我该如何解决?谢谢.

推荐答案

您不能为按钮动作处理程序指定自己的参数.它带有 1 个参数的变体将接受发件人",即触发操作的按钮.您的工作流程应该如下:

You cannot specify your own parameters to button action handler. Its variant with 1 parameter will accept "sender", that is button that triggered the action. Your workflow should be following:

  1. 查找单击按钮的单元格的索引路径
  2. 获取对应于该 indexPath 的 cellSight 对象
  3. 用来自 cellSite 对象的信息填充您的控制器.

(有点伪)代码可能看起来像,假设您有 SiteObject 数组:

(somewhat pseudo-)Code may look like, assuming you have array of SiteObjects:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell = ...  // create/init cell somehow

    [cell.selectedMapButton addTarget:self action:@selector(sightMapButton:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

- (void)sightMapButton: (UIButton *)sender
{
    NSIndexPath *indexPath = ... // Find indexPath of button
    SiteObject cellSight = sitesArray[indexPath.row];
    TOCGSightseeingMapKitViewController *mapKitController = [[TOCGSightseeingMapKitViewController alloc] init];
    mapKitController.sightCoordinates = cellSight[@"S_Coodinates"];

    [self.navigationController pushViewController:mapKitController animated:YES];
}

如何找到包含按钮的单元格的索引路径,例如在我的其他答案中

How to find indexPath of cell containing button you can find for example in my other answer

这篇关于如何使用按钮操作(带参数)从自定义 UITableViewCell 推送视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆