【问题标题】:How do I get different view controllers for a button click in custom table view cell如何为自定义表格视图单元格中的按钮单击获取不同的视图控制器
【发布时间】:2018-07-05 07:22:51
【问题描述】:

如何为通过自定义tableViewCell 提供的按钮中的不同点击添加viewController?我正在使用 Swift 4.0。

【问题讨论】:

  • 您想在点击表格视图单元格中的按钮时显示 Viewcontroller ????
  • 是的。自从我使用了表格视图以来,不同的视图控制器。

标签: ios swift xcode uitableview uibutton


【解决方案1】:

对于cellForRowAtIndexPath:方法根据索引路径为按钮添加标签

cell.yourbutton.tag = indexPath.row;

然后为按钮添加目标如下 -

[cell.yourbutton addTarget:self action:@selector(yourButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

斯威夫特:

cell.yourbutton.addTarget(self, action: #selector(ViewController. yourButtonClicked(_:)), for: .touchUpInside)

现在添加代码以根据标签类型在此处打开视图控制器,即您的单元格索引

-(void)yourButtonClicked:(UIButton*)sender
{
     if (sender.tag == 0) 
     {
         // open view controller for index zero
     }
}

斯威夫特-

@objc func yourButtonClicked(_ sender: UIButton)  {
     if sender.tag == 0
     {
        // open view controller for index zero
     }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多