2017-08-08 65 views
0

这让我很难过,我尝试了一些教程和堆栈答案的方法,但它仍然没有构建。桌面视图用segue单独查看

所以基本上:

  • 即时得到的核心数据,然后放置该成一个数组。 (正常工作)
  • 之后,我只是在细胞中显示的姓氏和名字(正常工作)对细胞
  • 用户点击看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) 
    } 
} 
+0

你的实际问题是什么?你说这段代码没有编译,但是你说你把打印语句用于调试,那么你会得到一个编译错误还是运行时错误? –

+0

主要使用打印语句来查看数据是否实际上被加载到它所在的变量中。但是它不会将它们发送到其他视图。我试过了: let currentCell = tableView.cellForRow(at:indexPath)!作为UITableViewCell 然后我想传递的变量以及这里讨论的内容 [链接](https://stackoverflow.com/questions/28430663/send-data-from-tableview-to-detailview-swift) –

回答

0

你是在设定的tableView的变量:cellForRowAt:indexPath:是局部变量和不可用的功能prepareForSegue:。尝试在顶部声明变量作为类的属性。