无法继续表视图

Unable to segue tableview

我正在尝试创建一个 Tableview 应用程序,这是我的代码,由于 cvc.college = colleges[indexPath.row] 说无法分配类型 ['string' 到 'College!' 我该怎么办?

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)


}

}

首先,您需要将 moveRowAtprepareForSegue 方法放在 ViewController class 中,因为目前您已将其添加为 class 之外.

之后你的 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的代码,所以我认为你已经创建了从UITableViewCellCollegeViewController的segue .