通过自定义UITableViewCell中的按钮执行搜索 [英] Performing a segue from a button within a custom UITableViewCell

查看:58
本文介绍了通过自定义UITableViewCell中的按钮执行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过自定义UITableViewCell中的按钮执行搜索,但是当我按下该按钮时,我不知道如何访问单元格的内容.我意识到可以通过didSelectRowAtIndexPath完成此操作,但是我有该方法执行另一项功能,并且我想使用在表格单元格中创建的按钮.

I want to perform a segue from a button within a custom UITableViewCell, but I don't know how to access the cell's contents when I push that button. I realize that this could be accomplished through didSelectRowAtIndexPath, however I have that method performing another function, and I would like to use the button I have created within the table cell.

这是我遇到问题的表演方法:

Here is my perform segue method I'm having trouble with:

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
    }
}

我在显示按钮时标记单元格中的按钮,并对其进行目标操作:

I tag the button in the cell when displaying it, and give it a target 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)
}

这是按下按钮时调用的动作:

Here is the action that is called when pressing the button:

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

感谢您的帮助.谢谢!

推荐答案

使用Method创建一个协议,该协议将由TableViewController上定义的CustomCell的Delegate调用

Create a Protocol with the Method, that will be called by the CustomCell's Delegate, defined on your TableViewController

//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上使用协议

Now use the Protocol on your UITableViewController

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 )

 }

}

在自定义单元格上定义代表,在新建"按钮内定义代表函数.

Define the Delegate on your Custom Cell and the Call inside your New Button the Delegate Function.

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)
           }
    }
}

希望这可以使您理解并实现代码中的内容清晰明了.

Hopefully this can be clean and clear for you to understand and implement in your code.

这篇关于通过自定义UITableViewCell中的按钮执行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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