Search code examples
swiftxcodeuitableviewswift3nsindexpath

Unable to segue tableview


I am trying to create a Tableview app and here is my code, I am unable to segue due to an error saying cvc.college = colleges[indexPath.row] saying cannot assign value of type 'string' to 'College!' What do i do?

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]

        }

}

heres my code for 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)


}

}

Solution

  • First of all you need to put moveRowAt and prepareForSegue methods inside the ViewController class because currently you have added it as outside the class.

    After that your prepareForSegue like this.

    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]
        }
    }
    

    Whole code would be like this.

    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]
            }
        }
    }
    

    Note: There is no code of self.performSegue, so I'm considering that you have created segue from UITableViewCell to CollegeViewController.