无法segue tableview
Posted
技术标签:
【中文标题】无法segue tableview【英文标题】:Unable to segue tableview 【发布时间】:2017-03-18 04:51:36 【问题描述】:我正在尝试创建一个 Tableview 应用程序,这是我的代码,由于错误提示 cvc.college = colleges[indexPath.row] 说无法将类型“字符串”的值分配给“学院! ' 我该怎么办?
import UIKit
class ViewController: UIViewController, UITableViewDelegate ,UITableViewDataSource
@IBOutlet weak var myTableView: UITableView!
var colleges = ["Harper","UofI","Florida State University"]
override func viewDidLoad()
super.viewDidLoad()
myTableView.delegate = self
myTableView.dataSource = self
var college1 = College(name: "Harper", state: "IL", population: "25,000+")
var college2 = College(name: "UofI", state: "IL", population: "44,000+")
var college3 = College(name: "Florida State University", state: "Fl", population:"40,000+")
var colleges = [college1 , college2, college3]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return colleges.count
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let myCell = myTableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
myCell.textLabel!.text = colleges[indexPath.row]
return myCell
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
if editingStyle == .delete
colleges.remove(at: indexPath.row)
tableView.reloadData()
let college = colleges[indexPath.row]
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
let college = colleges[sourceIndexPath.row ]
colleges.remove(at: sourceIndexPath.row)
colleges.insert(college, at: destinationIndexPath.row)
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
var cvc = segue.destination as! CollegeViewController
if let cell = sender as? UITableViewCell,
let indexPath = self.myTableView.indexPath(for: cell)
cvc.college = colleges[indexPath.row]
这是我的 CollegeViewController 代码
class CollegeViewController: UIViewController
var college : College!
@IBAction func onTappedSave(_ sender: UIButton)
college.name = collegeText.text!
college.state = stateText.text!
college.population = populationText.text!
@IBOutlet weak var collegeText: UITextField!
@IBOutlet weak var populationText: UITextField!
@IBOutlet weak var stateText: UITextField!
override func viewDidLoad()
super.viewDidLoad()
collegeText.text = college.name
stateText.text = college.state
populationText.text = String(college.population)
【问题讨论】:
【参考方案1】:首先,您需要将 moveRowAt
和 prepareForSegue
方法放在 ViewController
类中,因为目前您已将其添加到类之外。
之后你的prepareForSegue
就是这样。
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
var cvc = segue.destination as! CollegeViewController
if let cell = sender as? UITableViewCell,
let indexPath = self.myTableView.indexPath(for: cell)
cvc.college = colleges[indexPath.row]
整个代码是这样的。
import UIKit
class ViewController: UIViewController, UITableViewDelegate ,UITableViewDataSource
@IBOutlet weak var myTableView: UITableView!
var colleges = ["Harper","UofI","Florida State University"]
override func viewDidLoad()
super.viewDidLoad()
myTableView.delegate = self
myTableView.dataSource = self
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return colleges.count
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let myCell = myTableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
myCell.textLabel!.text = colleges[indexPath.row]
return myCell
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
if editingStyle == .delete
colleges.remove(at: indexPath.row)
tableView.reloadData()
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
let college = colleges[sourceIndexPath.row ]
colleges.remove(at: sourceIndexPath.row)
colleges.insert(college, at: destinationIndexPath.row)
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
var cvc = segue.destination as! CollegeViewController
if let cell = sender as? UITableViewCell,
let indexPath = self.myTableView.indexPath(for: cell)
cvc.college = colleges[indexPath.row]
注意:没有self.performSegue
的代码,所以我认为你已经创建了从UITableViewCell
到CollegeViewController
的segue。
【讨论】:
低于列出人口和州的大学会发生什么?他们必须被删除吗? @AdrianAtun 由您决定,因为当前您没有使用它,如果您正在使用自定义对象的colleges
数组设置数据源数组,那么如果您在 ViewController 中添加该数组也将是击球手类。
@AdrianAtun 你有没有像我的回答一样尝试过,之后你能在CollegeViewController
中传递数据吗?
还没有,我的mac在学校,我的个人电脑用的是windows。
@AdrianAtun 好的,当你尝试它时,在这里回复它是否有效。以上是关于无法segue tableview的主要内容,如果未能解决你的问题,请参考以下文章