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)
}
}
你的實際問題是什麼?你說這段代碼沒有編譯,但是你說你把打印語句用於調試,那麼你會得到一個編譯錯誤還是運行時錯誤? –
主要使用打印語句來查看數據是否實際上被加載到它所在的變量中。但是它不會將它們發送到其他視圖。我試過了: let currentCell = tableView.cellForRow(at:indexPath)!作爲UITableViewCell 然後我想傳遞的變量以及這裏討論的內容 [鏈接](https://stackoverflow.com/questions/28430663/send-data-from-tableview-to-detailview-swift) –