我想区分一下通过prepareForSegue发送的内容。
例如,如果顶部的单元格在tableView (indexPath.row == 0
)上,我想通过firstVariable发送,如果是indexPath.row == 1
,我想通过secondVariable发送,依此类推。
我已经尝试过了,但没有成功:
let starterInfoSegueIdentifier = "ToStarterInfoSegue"
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
if indexPath.row == 0
{
func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == starterInfoSegueIdentifier
{
let destination = segue.destination as! Starter2ViewController
let starterIndex = tableView.indexPathForSelectedRow?.row
destination.name = startersArray[starterIndex!]
destination.variable = firstVariable
}
}
}
}
编辑:
以下是更新后的代码:
class StartersViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let startersArray = ["ArrayItem1", "ArrayItem2"]
var firstVariable = 1
var secondVariable = 10
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return startersArray.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 150
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "StartersCell") as! StartersTableViewCell
//Set round corners
cell.cellView.layer.cornerRadius = cell.layer.frame.height / 2
cell.startersLabel.text = startersArray[indexPath.row]
return cell
}
let starterInfoSegueIdentifier = "ToStarterInfoSegue"
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
self.performSegue(withIdentifier:starterInfoSegueIdentifier,sender:indexPath.row)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == starterInfoSegueIdentifier
{
let destination = segue.destination as! Starter2ViewController
let index = sender as! Int
destination.name = startersArray[index]
destination.counter = index == 0 ? firstVariable : secondVariable
}
}
}
tableViewCell
的代码如下:
import UIKit
class StartersTableViewCell: UITableViewCell {
@IBOutlet weak var cellView: UIView!
@IBOutlet weak var startersLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}
发布于 2018-06-07 03:49:42
Prepare必须是类范围
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
self.performSegue(withIdentifier:starterInfoSegueIdentifier,sender:indexPath.row)
}
//
func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == starterInfoSegueIdentifier
{
let destination = segue.destination as! Starter2ViewController
let index = sender as! Int
destination.name = startersArray[index]
destination.variable = index == 0 ? firstVariable : secondVariable
}
}
如果您有很多变量,那么我建议您创建一个struct
来包含您的模型,然后在startersArray
中添加它的对象,并从该结构的对象的属性进行访问
struct MyContent {
var name:String
var otherVar:String
}
然后将其重构为
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
self.performSegue(withIdentifier:starterInfoSegueIdentifier,sender:startersArray[indexPath.row])
}
//
func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == starterInfoSegueIdentifier
{
let destination = segue.destination as! Starter2ViewController
let item = sender as! MyContent
destination.name = item.name
destination.variable = item.otherVar
}
}
发布于 2018-06-08 01:29:44
我看到了问题和答案,indexPath或indexPath.row被作为performSegue(withIdentifier)函数的任何参数传递。
我认为最简单的方法是定义类变量,比如startersArrayIndexToPass,来存储要传递的项的索引。将此变量存储在tableview(didSelectRowAt)方法中,并在prepare(segue:)函数中使用。这种方法避免了将已知类型转换为Any类型,然后再从Any类型转换为已知类型。
https://stackoverflow.com/questions/50728395
复制相似问题