ios - 从自定义 UITableViewCell 中的按钮执行 segue

标签 ios uitableview swift

我想从自定义 UITableViewCell 中的按钮执行 segue,但我不知道在按下该按钮时如何访问单元格的内容。我意识到这可以通过 didSelectRowAtIndexPath 来完成,但是我有该方法执行另一个功能,我想使用我在表格单元格中创建的按钮。

这是我遇到问题的 perform segue 方法:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    if segue.identifier == "showComments"
    {
        let vc:CommentOnPostViewController = segue.destinationViewController as CommentOnPostViewController

        var buttonPosition:CGPoint = sender?.convertPoint(CGPointZero, toView: self.feed) as CGPoint!
        var path:NSIndexPath = self.feed.indexPathForRowAtPoint(buttonPosition) as NSIndexPath!

        var postToCommentOn:PFObject = feedList[path.row] as PFObject
        vc.post = postToCommentOn as PFObject
    }
}

我在显示的时候在单元格中标记了按钮,并给它一个目标 Action :

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    // There is code that goes here for creating and displaying the cell.

    cell.commentButton.tag = indexPath.row
    cell.commentButton.addTarget(self, action: "addComment", forControlEvents: UIControlEvents.TouchUpInside)
}

这是按下按钮时调用的 Action :

func addComment()
{
    self.performSegueWithIdentifier("showComments", sender: self)
}

感谢任何帮助。谢谢!

最佳答案

使用方法创建一个协议(protocol),该方法将由在您的 TableViewController 上定义的 CustomCell 的委托(delegate)调用

//Pass any objects, params you need to use on the 
//segue call to send to the next controller.

protocol MyCustomCellDelegator {
    func callSegueFromCell(myData dataobject: AnyObject)
}

现在在你的 UITableViewController 上使用协议(protocol)

class MyTableViewController : UITableViewController, MyCustomCellDelegator {


 //The usual Defined methods by UIViewController and UITableViewController 

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

  //Set the CustomCell new Delegate
  var cell = tableView.dequeueReusableCellWithIdentifier(customIdentifier) as MyCustomCell


  cell.delagete = self

  return cell

 }


 //MARK: - MyCustomCellDelegator Methods

 func callSegueFromCell(myData dataobject: AnyObject) {
   //try not to send self, just to avoid retain cycles(depends on how you handle the code on the next controller)
   self.performSegueWithIdentifier("showComments", sender:dataobject )

 }

}

在自定义单元格上定义委托(delegate),并在新按钮内调用委托(delegate)函数。

class MyCustomCell : UITableViewCell {

      var delegate:MyCustomCellDelegator!
      @IBOutlet weak var myButton:UIButton



     @IBAction func buttonPressed(sender:AnyObject){
           var mydata = "Anydata you want to send to the next controller"
           if(self.delegate != nil){ //Just to be safe.
             self.delegate.callSegueFromCell(mydata)
           }
    }
}

希望这对于您理解和在您的代码中实现是清晰明了的。

关于ios - 从自定义 UITableViewCell 中的按钮执行 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26880526/

相关文章:

ios - 为什么我的图像没有显示在 UIImageView 中?

ios - 使用按钮快速循环遍历数组项

ios - xCode/Firebase 搜索栏单元格失败

ios - 界面测试失败 : Failed to update to requested orientation

ios - 自定义 Swift UIGestureRecognizer 不调用分配的操作

ios - 什么类型的 HealthKit 单元用于 VO2 Max 测量? - swift 4

ios - 移动 uiimageview 动画

ios - AVPlayer 视频重力定位?

ios - 如何使用 Objective-C 显示多个自定义单元格?

ios - 为 UITableViewController 解除 segue?