【问题标题】:Table View To seperate view with segue表格视图用segue分隔视图
【发布时间】: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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多