2017-03-18 2 views
0

J'essaie de créer une application Tableview et voici mon code, je suis incapable de segue en raison d'une erreur disant cvc.college = collèges [indexPath.row] dire ne peut pas attribuer de valeur de type 'chaîne' au collège!' Que dois-je faire?Impossible de segmenter tableview

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 mon code pour 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) 


} 

} 

Répondre

0

Tout d'abord vous devez mettre moveRowAt et prepareForSegue méthodes dans la classe ViewController car actuellement vous avez ajouté comme en dehors de la classe.

Ensuite, votre prepareForSegue comme ça.

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

Tout le code serait comme ceci.

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: Il n'y a pas de code de self.performSegue, donc je considère que vous avez créé Segue UITableViewCell-CollegeViewController.

+0

Qu'adviendrait-il des collèges situés en dessous de la liste de la population et de l'État? Devraient-ils être supprimés? –

+0

@AdrianAtun C'est à vous parce que vous ne l'utilisez pas actuellement, Si vous définissez un tableau de sources de données avec ce tableau 'colleges' d'objets personnalisés, alors c'est pâte si vous ajoutez ce tableau également dans votre classe ViewController. –

+0

@AdrianAtun Avez-vous essayé comme ma réponse est-ce que cela fonctionne après que vous êtes en mesure de transmettre des données dans 'CollegeViewController'? –