swift - 从自定义原型(prototype)单元格中的 UIButton PrepareForSegue

标签 swift uitableview uibutton uistoryboardsegue custom-cell

正如标题所说,我有一个带有原型(prototype)单元格的 tableView; cell 是一个自定义单元格(所以我创建了一个名为 CustomCell.swift 的类,我为图像、标签、按钮等创建了 IBOutlet);这是我的课

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

哦,首先我通过控制权从按钮到第二个 Controller 进行了转场:也许我必须从 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).

或者,您可以在自定义单元格类中创建一个 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
        }
    }
}

关于swift - 从自定义原型(prototype)单元格中的 UIButton PrepareForSegue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32341629/

相关文章:

ios - 通过浮点值将按钮图像从一个图像淡化到另一个图像

ios - 当我触摸时,UIButton 突出显示不起作用

ios - 更改按钮图像时,按钮大小也会更改。为什么?

ios - ObjectMapper 在将对象转换为字典期间删除了没有值的字段

swift - 这是 UITextView 透明度错误吗?

ios - 如何使用enum获取字符串值

ios - 子类 UITableViewHeaderFooterView 高度未注册

ios - 删除 TableView iOS 7 上的色调

ios - 在 UITableViewCell 中动态布置 subview

ios - 为什么不能将 UITableViewCell 实例用作计算属性?