【发布时间】:2017-08-08 14:10:15
【问题描述】:
这让我很困惑,我尝试了几种方法来做教程和堆栈答案,但它仍然没有建立。
所以基本上:
- 我正在获取核心数据,然后将其放入数组中。 (工作正常)
- 之后我只在单元格中显示名字和姓氏(工作正常)
- 用户点击单元格以查看运动员详细信息视图
我已经放置了一些打印语句,看看哪里出了问题。
提前感谢您的意见。
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)
}
}
【问题讨论】:
-
您的实际问题是什么?你说这段代码没有编译,然后你说你把打印语句用于调试,那么你会得到编译或运行时错误还是根本没有错误?
-
主要使用打印语句来查看数据是否真正被加载到它所在的变量中。但是,它不会将它们发送到另一个视图。我试过这样做:让 currentCell = tableView.cellForRow(at: indexPath)!作为 UITableViewCell 然后我想传递的变量以及这里讨论的内容link
标签: arrays uitableview core-data swift3