Ios 如何在从nib创建的UITableViewCell中的按钮上触发segue操作?

Ios 如何在从nib创建的UITableViewCell中的按钮上触发segue操作?,ios,swift,Ios,Swift,现在,我正在从nib文件创建一个表视图单元格。我在这个nib文件中有一个按钮。我希望此按钮触发序列图像板上特定视图控制器的序列 看起来很简单,除了这个nib文件在我的整个应用程序的不同视图控制器上的许多不同表中使用,我希望按钮在每个实例中链接到相同的目标视图控制器。此外,目标视图控制器还包含一个将此nib文件作为单元格的表,这意味着它还需要链接到自身 出于这些原因,我希望有一个解决方案包含上述nib文件的UITableViewCell子类的代码。(但我怀疑这是可能的。) Instagram就是我

现在,我正在从nib文件创建一个表视图单元格。我在这个nib文件中有一个按钮。我希望此按钮触发序列图像板上特定视图控制器的序列

看起来很简单,除了这个nib文件在我的整个应用程序的不同视图控制器上的许多不同表中使用,我希望按钮在每个实例中链接到相同的目标视图控制器。此外,目标视图控制器还包含一个将此nib文件作为单元格的表,这意味着它还需要链接到自身

出于这些原因,我希望有一个解决方案包含上述nib文件的UITableViewCell子类的代码。(但我怀疑这是可能的。)

Instagram就是我想要的功能的一个很好的例子。想想Instagram上的几个不同选项卡是如何在表格中显示如下单元格的:

无论您在哪个选项卡中,单击此单元格左上角的用户名都会将您转换到用户视图控制器,即使您已经在用户视图控制器中:


使用addTarget按钮

将此代码放入tableView的cellForRowAt方法中

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    guard let cell = tableView.dequeueReusableCell(withIdentifier: "YOUR_ID", for: indexPath) as? InstagramCell else { return }
    cell.nasaBtn.addTarget(self, action: #selector(firstViewController.nasaBtnPressed(_:)), for: .touchUpInside)
    return cell

}

@objc func nasaBtnPressed(_ sender: UIButton) {
    performSegue(withIdentifier: "NASA_PROFILEID", sender: nil)
}
在这里,您可以执行以下操作:

1) 不需要提供标签 2) 获取按钮位置并根据该位置执行任务 3) ProductStarImage//是我的按钮名称

步骤1:

 //IBOutlets
    @IBOutlet weak var CategoriesTableView: UITableView!

 //TableCell identifier
    let cellReuseIdentifier = "Cell"
    var cell = CustomTableCellClass()
步骤2:在TableView的cellForRowAt中

  //setting cell data
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = self.CategoriesTableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! CustomTableCellClass

    cell.layer.shadowColor = UIColor.gray.cgColor
    cell.layer.shadowRadius = 2

    //add a action to button 
    cell.ProductStarImage.addTarget(self, action: #selector(CategoriesVC.FavouriteButtonHandler(sender:)), for: .touchUpInside)


    return cell
}
步骤2:按钮处理程序

//Favourite button Action Handler
    @objc func FavouriteButtonHandler (sender: UIButton) {

        //assign current cell to global cell declared //dequeue
        cell = self.CategoriesTableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! CustomTableCellClass
        //get CGPoint of button selected
        let buttonPosition : CGPoint = sender.convert(CGPoint.zero, to: self.CategoriesTableView)
        //get indexPath from CGPoint of Button
        let indexPath : IndexPath = self.CategoriesTableView.indexPathForRow(at: buttonPosition)!
        //Now assign selected cell indexPath to Cell declared globally 
        cell = self.CategoriesTableView.cellForRow(at: indexPath)! as! CustomTableCellClass


    //here perform action required 
    like - cell.ProductStarImage //any action

    self.CategoriesTableView.reloadRows(at: [indexPath], with: .none)
}

您可以使用“创建协议”,该协议将返回要导航到的所需ViewController。您只需要使自定义单元格符合协议,并返回所需的视图控制器或情节提要标识符。在
didSelect
方法中完成后,您可以尝试将其转换为该协议。如果成功,则推送所需的视图控制器。检查下面的代码,我正在使用情节提要标识符获取所需的视图控制器

protocol CustomCellNavigator {
    var identifier: String { get }
}

class YourViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var myTableView: UITableView!

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "YourIdentifier", for: indexPath) as! CustomCell
        cell.destinationStoryBoardIdentifier = "YourIdentifier"
        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let cell = myTableView.cellForRow(at: indexPath) as? CustomCellNavigator {
            if let viewcontroller = storyboard?.instantiateViewController(withIdentifier: cell.identifier) {
                navigationController?.pushViewController(viewcontroller, animated: true)
            }
        }
    }
}

class CustomCell: UITableViewCell, CustomCellNavigator {

    var identifier: String {
        get { return destinationStoryBoardIdentifier }
    }

    // You can replace with whatever logic you want to use for
    // finding the required viewcontroller
    // I have just used a storyboard identifier here
    var destinationStoryBoardIdentifier = "YourViewController"
}

在nib文件中为该按钮定义segue。