无法看到tableview

时间:2017-03-18 04:51:36

标签: swift xcode uitableview swift3 nsindexpath

我正在尝试创建一个Tableview应用程序,这里是我的代码,由于错误说cvc.college = schools [indexPath.row]说无法分配类型'字符串&#39的值,我无法定位;去大学!' 我该怎么办?

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 个答案:

答案 0 :(得分:0)

首先,您需要将moveRowAtprepareForSegue方法放在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的代码,因此我认为您已经创建了从UITableViewCellCollegeViewController的segue。

相关问题