表视图用segue分隔视图

时间:2017-08-08 14:10:15

标签: arrays uitableview core-data swift3

这让我很难过,我已经尝试了几种方法来做教程和堆栈答案,但它仍然没有建立。

基本上是这样的:

  • 我获取核心数据,然后将其放入数组中。 (工作正常)
  • 之后,我只在单元格中显示名字和姓氏(工作正常)
  • 用户点击单元格查看athleteDetalView

我已经放了一些印刷语句来查看它出错的地方。

先谢谢您的意见。

import UIKit
import CoreData

class athleteViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

//labels
@IBOutlet weak var athleteLabel: UILabel!

//buttons
@IBOutlet weak var athleteCreate: UIBarButtonItem!

@IBOutlet weak var athleteTableView: UITableView!

var athleteArray:[Athlete] = []
var myIndex = 0


override func viewDidLoad() {
    super.viewDidLoad()

    athleteTableView.delegate = self
    athleteTableView.dataSource = self

    self.fetchData()
    self.athleteTableView.reloadData()

}


func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return athleteArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "athleteName", for: indexPath)
    let name = athleteArray[indexPath.row]
    cell.textLabel!.text = name.firstName! + " " + name.lastName!

    let athleteName = name.firstName!
    let lastName = name.lastName!
    let age = name.age!
    let sport = name.sport!
    let email = name.email!

    print(athleteName)
    print(lastName)
    print(age)
    print(sport)
    print(email)

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    myIndex = indexPath.row
    let currentCell



    //let storyboard = UIStoryboard(name: "Main", bundle: nil)
    //let destination = storyboard.instantiateViewController(withIdentifier: "athleteDetailsViewController") as! athleteDetailsViewController

    //let athName = athleteArray[myIndex]

    //testdata


    //test data


    performSegue(withIdentifier: "athleteDetailsSegue", sender: self)



    //self.navigationController?.pushViewController(destination, animated: true)


}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if (segue.identifier == "athleteDetailsSegue") {
        var destination = segue.destination as! athleteDetailsViewController

        destination.firstNameString = athleteName
        destination.lastNameString = lastName
        destination.ageString = age
        destination.sportString = sport
        destination.emailString = email
        //destination.getImage = name.image
    }



}



func fetchData(){

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    do{
        athleteArray = try context.fetch(Athlete.fetchRequest())
    }
    catch{
        print(error)
    }
}

1 个答案:

答案 0 :(得分:0)

您在tableView中设置的变量:cellForRowAt:indexPath:是局部变量,在函数prepareForSegue:中不可用。尝试将顶部的变量声明为类的属性。