从自定义原型单元格中的 UIButton PrepareForSegue

PrepareForSegue from a UIButton in a custom prototype cell

正如标题所说,我有一个带有原型单元格的 tableView; cell 是一个自定义单元格(所以我在 中创建了一个名为 CustomCell.swift 的 class 我为图像、标签、按钮等创建了 IBOutlet);这是我的 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
    }
}

然后我做了 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)
    }
}

好的,好的,困难的部分来了:制作 prepareForSegue;我不知道如何解决这个问题,我试过了

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

但我不知道如何设置常量indexPath

哦,首先我通过 control-right 从按钮到第二个控制器进行了转场:也许我必须从 tableView 单元格进行转场???

希望有人能帮助我!

你可以像这样获取单元格的索引路径

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

您还需要像这样更改 makeSegue

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

并且在您的 cellForRowAtIndexPath 中只需将设置操作的行更改为 cell.button.addTarget(self, action: "makeSegue:", forControlEvents: UIControlEvents.TouchUpInside)

或者,您可以在自定义单元格 class 中创建一个 squadra 属性 来保存该单元格的 arrayCori 值,这样您就有了一些代码看起来像这样:

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

故事板 + prepareForSegue

通过在 故事板.

中为按钮添加单独的 UIStoryboardSegue 和自己的标识符,几乎不需要任何代码即可完成。

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 and additional details on Swift Recipes 上找到此解决方案。

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

/* 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]
        }
    }
}