gpt4 book ai didi

ios - UITableView 导航的问题,在 segue 上传递数据

转载 作者:行者123 更新时间:2023-11-29 05:53:22 25 4
gpt4 key购买 nike

我无法将数据从 UITableView 传递到 UIViewController。尝试过多种方法但均失败。出现此错误

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVO)

当我运行该项目时。

请参阅下面的代码,了解我所尝试过的内容。

//Code on the UITableViewController

class ShonaHymnsUITableViewController: UITableViewController {
var hymns : [Hymns] = Hymns.fetchHymns()

//MARK: - UItableViewDataSource
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return hymns.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "hymnCell", for: indexPath) as! HymnsTableViewCell

let hymn = hymns[indexPath.row]
cell.hymn = hymn
return cell
}

// MARK: - NAVIGATION
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
self.performSegue(withIdentifier: "ShowHymnDetail", sender: indexPath)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// TODO: Prepare for Hymn Detal segue
if segue.identifier == "ShowShonaHymnListing" {
let shonaHymnListingVC = segue.destination as! ShonaHymnsUITableViewController
shonaHymnListingVC.hymns = hymns

} else if segue.identifier == "ShowHymnDetail" {

let indexPath = tableView.indexPathForSelectedRow
let selectedHymn = hymns[(indexPath?.row)!]
let hymnDetailVC = segue.destination as! ShonaHymnsViewController
hymnDetailVC.hymn = selectedHymn
}
}
}

//Code on the ViewController that I want to receive the data
class ShonaHymnsViewController: UIViewController {


@IBOutlet weak var hymnNumber: UILabel!
@IBOutlet weak var hymnTitle: UILabel!
@IBOutlet weak var hymnDetail: UITextView!

var hymn: Hymns!

override func viewDidLoad() {
super.viewDidLoad()

hymnNumber.text = hymn.hymnNumber
hymnTitle.text = hymn.hymnTitle
hymnDetail.text = hymn.hymnDetail

}

}

这是一个赞美诗书应用程序,我希望在接收者 View Controller 上显示完整的赞美诗详细信息。 See image for the setup of the app

最佳答案

颠倒顺序

self.performSegue(withIdentifier: "ShowHymnDetail", sender: indexPath)
tableView.deselectRow(at: indexPath, animated: true)

为此

hymns[(indexPath?.row)!]

有一个有效的值

关于ios - UITableView 导航的问题,在 segue 上传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55445512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com