swift - 如何通过 tableViewDelegate 进行转场

标签 swift delegates protocols segue

我想创建一个从 tableViewDelegate 单元格到另一个 View 的转场。这是我的代码:

import UIKit

class CoriViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
    @IBOutlet var tableView: UITableView!

var elencoCori : SquadraModel! //This var contains label and array

override func viewDidLoad()
{
    super.viewDidLoad()

    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

    InterfaceManager.sharedInstance.putBlurBackgroundToTableView(self.view, tableView: self.tableView)
    self.tableView.tableFooterView = UIView(frame:CGRectZero)
    self.tableView.backgroundColor = UIColor.clearColor()
}

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return self.elencoCori.cori.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell : UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
    let coro = elencoCori.cori[indexPath.row]
    cell.textLabel?.text = coro.marker
    cell.backgroundColor = UIColor.clearColor()
    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

    return cell
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)
{
    self.performSegueWithIdentifier("toDettaglioController", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
}

现在的问题是:如何设置发送到 secondView 的 prepareForSegue?在其他情况下,我会这样解决问题:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    if segue.identifier == "toDettaglioController"
    {
        if let indexPath = tableView.indexPathForSelectedRow()
        {
            let controller = segue.destinationViewController as! Dettaglio controller
            controller.coro = elencoCori.cori[indexPath.row]
        }
    }
}

但我知道这行不通,我必须使用委托(delegate)方法。那么每个人都可以帮助我提供分步指南吗?

最佳答案

在您的 didSelectRowAtIndexPath 方法上调用 performSegueWithIdentifier 并将 indexPath 作为您的发件人传递。

然后在 prepareForSegue 中,您可以执行 if let indexPath = sender as NSIndexPath 并设置您的 Controller 。

关于swift - 如何通过 tableViewDelegate 进行转场,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31788529/

相关文章:

ios - Swift 子类与协议(protocol)

ios - Swift - 显示另一个带有导航栏的 View Controller

swift - dequeueReusableCell 在 3 个单元格后不出队

ios - 滚动时不会调用 estimatedHeightForRowAt

iphone - 如何在1个viewController中管理2个tableview?

objective-c - 使用 Swift 数组 - 如何实现符合协议(protocol)的对象数组

swift - 多类型约束

arrays - 在 Swift 中对数组进行分组和排序

c# - BeginInvoke/EndInvoke 是在主线程上调用的好习惯吗?

ios - 尝试使用 -(BOOL) textFieldShouldReturn :(UITextField *)textField; to make the keyboard go away in a subview