gpt4 book ai didi

swift - 准备 segue 为 indexPathForSelectedRow 返回 nil?

转载 作者:行者123 更新时间:2023-11-28 10:51:12 35 4
gpt4 key购买 nike

我想根据在我的 tableView 中选择的行,将信息从一个 viewController 的数组传递到另一个。为此,我实现了以下功能:

func tableView(_ messagesListTableView: UITableView, didSelectRowAt indexPath: IndexPath) {
messagesListTableView.deselectRow(at: indexPath, animated: true)
performSegue(withIdentifier: "individualMessageSegue", sender: self)
print(indexPath)//returns [0,0]... meaning the first row
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "individualMessageSegue" else{return}

if let indexPath = self.messagesListTableView.indexPathForSelectedRow{
let openedMessageConversation = fullOrderedMessageArray[(indexPath.row)] //cause app to crash because nil value
let destViewController: IndividualMessageVC = segue.destination as! IndividualMessageVC
destViewController.messageConvo = openedMessageConversation
}else{
print("it was nil")
}

}

我相信我已经正确设置了所有内容,但是尽管 didSelectRowAt 的打印语句总是返回 [0,0] 的 indexPath 的值(在表只有一个部分和一行),prepare 中的打印语句总是告诉我 indexPathnil。如果我删除 if let 条件,应用程序将崩溃,因为 fullOrderedMessageArray[(indexPath.row)] 包含 nil 值。我不知道为什么它总是为一个函数返回 nil 而另一个函数不返回。

最佳答案

你能试试这个吗。

func tableView(_ messagesListTableView: UITableView, didSelectRowAt indexPath: IndexPath) {
messagesListTableView.deselectRow(at: indexPath, animated: true)
performSegue(withIdentifier: "individualMessageSegue", sender: indexPath)
print(indexPath)//returns [0,0]... meaning the first row
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "individualMessageSegue" else{return}

if let indexPath = sender as? IndexPath {
let openedMessageConversation = fullOrderedMessageArray[(indexPath.row)]
let destViewController: IndividualMessageVC = segue.destination as! IndividualMessageVC
destViewController.messageConvo = openedMessageConversation
}else{
print("it was nil")
}

}

关于swift - 准备 segue 为 indexPathForSelectedRow 返回 nil?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46067544/

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