我想创建一个从 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/