PrepareForSegue from a UIButton in a custom prototype cell

时间:2015-09-01 22:07:43

标签: swift uitableview uibutton uistoryboardsegue custom-cell

As the title say I have a tableView with prototype cell; cell is a custom cell (so I made a class called CustomCell.swift in witch I created the IBOutlet for image, label, button etc); here my class

import UIKit

class CustomCell: UITableViewCell
{
    @IBOutlet var imageSquadra: UIImageView!
    @IBOutlet var button: UIButton!

    override func awakeFromNib()
    {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

then I made the UITableViewController:

import UIKit

class SquadreController: UITableViewController
{
    var index: NSIndexPath?
    var isScrolling = Bool()

    override func viewDidLoad()
    {
        super.viewDidLoad()

        DataManager.sharedInstance.createCori()

    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int
    {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return DataManager.sharedInstance.arrayCori.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
        let squadra = DataManager.sharedInstance.arrayCori[indexPath.row]

        cell.backgroundColor = UIColor.clearColor()

        if (indexPath.row % 2 == 0)
        {
            cell.backgroundColor = UIColor.greenColor()
        }
        else
        {
            cell.backgroundColor = UIColor.blueColor()
        }

        //Here I added target to the button in the cell, and below in the class I implemented the fun makeSegue()
        cell.button.addTarget(self, action: "makeSegue", forControlEvents: UIControlEvents.TouchUpInside)

        return cell
    }


    //Following 4 method are used to detect UIScollView scrolling and to change cell height.
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        index = indexPath
        tableView.beginUpdates()
        tableView.endUpdates()
    }

    override func scrollViewWillBeginDragging(scrollView: UIScrollView)
    {
        isScrolling = true
        tableView.beginUpdates()
        tableView.endUpdates()
    }

    override func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool)
    {
        isScrolling = false
    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
    {
        if isScrolling
        {
            return 100
        }

        if index == indexPath
        {
            return 200
        }
        else
        {
            return 100
        }
    }

    //Here I implemented the makeSegue() func, the action I had made as target of the button.
    func makeSegue() {
        self.performSegueWithIdentifier("toCoriViewController", sender: self)
    }
}

Ok ok now comes the hard part: to make the prepareForSegue; I do not have any ideas how to solve this problem, I tried

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
    {
        if segue.identifier == "toCoriViewController"
        {
            if let indexPath = ?????????????
            {
                let controller = segue.destinationViewController as! CoriViewController
                controller.coriSquadra = DataManager.sharedInstance.arrayCori[indexPath.row]
            }
        }
    }

but I don't know how to set the constant indexPath.

Oh, first of all I made a segue by control-right from the button to the second controller: maybe I must make that segue from the tableView cell???

Hope someone could help me!

3 个答案:

答案 0 :(得分:3)

You could get the index path of the cell like so

let indexPath : NSIndexPath
if let button = sender as? UIButton {
    let cell = button.superview?.superview as! UITableViewCell
    indexPath = self.tableView.indexPathForCell(cell)
}

You'll also need to change your makeSegue like this:

func makeSegue(button:UIButton) {
    self.performSegueWithIdentifier("toCoriViewController", sender: button)
}

and in your cellForRowAtIndexPath just change the line where you set the action to cell.button.addTarget(self, action: "makeSegue:", forControlEvents: UIControlEvents.TouchUpInside).

Alternatively, you could create a squadra property inside your custom cell class to hold the arrayCori value of that cell, so you'd have some code that looks like this:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
    let squadra = DataManager.sharedInstance.arrayCori[indexPath.row]
    cell.squadra = squadra #add this line
    cell.backgroundColor = UIColor.clearColor()
    if (indexPath.row % 2 == 0)
    {
        cell.backgroundColor = UIColor.greenColor()
    }
    else
    {
        cell.backgroundColor = UIColor.blueColor()
    }

    //Here I added target to the button in the cell, and below in the class I implemented the fun makeSegue()
    cell.button.addTarget(self, action: "makeSegue", forControlEvents: UIControlEvents.TouchUpInside)

    return cell
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    if segue.identifier == "toCoriViewController"
    {
        let controller = segue.destinationViewController as! CoriViewController
        if let button = sender as? UIButton {
            let cell = button.superview?.superview as! CustomCell
            controller.coriSquadra = cell.squadra
        }
    }
}

答案 1 :(得分:2)

故事板+ prepareForSegue

通过在 Storyboard 中添加一个单独的UIStoryboardSegue及其自己的按钮标识符,几乎不需要代码即可完成。

enter image description here

prepareForSegue变为:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if "fromButtonToViewController" == segue.identifier {
        if let button = sender as? UIButton {
            // ... The UIButton is the sender
        }
    }
}

这可以同时处理单击单元格并轻触按钮,将正确的sender传递给prepare:segue:sender,从而允许自定义单元格,按钮,过渡以及最终目标视图。这个陈述的演示可以在下面的紧凑项目中找到。

►在GitHub上找到此解决方案,并在Swift Recipes上找到其他详细信息。

答案 2 :(得分:1)

对于那些正在寻找通用方法的人。

/* Generic function to get uitableviewcell from any UIKit controllers which stored in deep level of views or stackviews */

func getCell<T>(_ view: T) -> UITableViewCell? {

    guard let view = view as? UIView else {
        return nil
    }

    return view as? UITableViewCell ?? getCell(view.superview)
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    if segue.identifier == "toCoriViewController"
    {
        if let button = sender as? UIButton,
            let cell = getCell(button),
            let indexPath = tableView.indexPath(for: cell)
        {
            let controller = segue.destinationViewController as! CoriViewController
            controller.coriSquadra = DataManager.sharedInstance.arrayCori[indexPath.row]
        }
    }
}