在表格视图单元格执行转场时出现问题

3
我目前正在学习Swift,并尝试在应用程序呈现的表格视图单元格之一被用户点击时执行转场。目前,每当用户执行此操作时,下一个视图控制器成功加载,但由于某种原因,我无法访问其任何UI元素,每次尝试这样做时,都会出现以下错误:
致命错误:在解包可选值时意外发现nil
该错误指向我尝试修改下一个视图控制器上显示的标签之一文本的行。
这是didSelectRowAt函数:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
    self.performSegue(withIdentifier: "segue1", sender: self)
}

这是 prepareForSegue 函数:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "segue1" {
        let destinationVC = segue.destination as! UserViewController
        let selectedRow = tableView.indexPathForSelectedRow!
        let selectedCell = tableView.cellForRow(at: selectedRow) as! CustomCell

        destinationVC.usernameLabel.text = selectedCell.userName.text //this is where the error is pointing to
        destinationVC.bioLabel.text = selectedCell.bio.text
        destinationVC.userImage.image = selectedCell.photo.image

    }
}

我不知道是什么原因导致了这个问题。我的目标是将从被点击的单元格中获取的数据传递到下一个视图控制器,但显然这样做会出现问题。有人知道如何解决吗?先谢谢了。


嗨,Mike。你是在使用静态还是动态的UITableView单元格? - Justin Rose
我正在使用动态单元格。 - Mike Vaugh
@Mike Vaugh,使用 destinationVC.usernameLabel.text = selectedCell.userName.text ?? " " - aircraft
@aircraft 尝试过了,还是同样的错误。 - Mike Vaugh
@Mike Vaughn,您是否记录了selectedRow?如果它是nil?如果您使用了取消选择操作。 - aircraft
显示剩余2条评论
2个回答

2

注意:我假设userNamebio都是UITextField

为什么不尝试这样做呢?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "segue1" {
        let destination = segue.destination as! UserViewController
        // Use of optional binding to make sure an indexPath exists
        if let indexPath = tableView.indexPathForSelectedRow {
            let cell = tableView.cellForRow(at: IndexPath(row: indexPath.row, section: indexPath.section)) as! CustomCell
            // Notice how we are not directly updating the label as before.
            destination.usernameText = cell.userName?.text
            destination.bioText = cell.bio?.text
        }
    }

}

现在在UserViewController中:
@IBOutlet weak var usernameLabel: UILabel!
@IBOutlet weak var bioLabel: UILabel!
// What we will be passing the text to instead.
var usernameText: String?
var bioText: String?

override func viewDidLoad() {
    super.viewDidLoad()
    // update the labels with the text from the proper cell.
    usernameLabel?.text = usernameText
    bioLabel?.text = bioText
}

您可以对图片执行相同的操作,只需要使用不同的类型。这与在prepare(for segue:)中使用时未分配出口有关。


伙计,你修好了!现在它运作得非常完美。非常感谢。我很快会接受你的答案。 - Mike Vaugh
太棒了!祝你的开发工作顺利。@MikeVaugh - Justin Rose

0
我在尝试使用UICollectionView时,prepare for segue方法出现了很大的问题。这两个非常相似,所以你应该能够轻松地将collectionview更改为tableview。
这是我所做的...使用变量selectedPack
在您想要segue到的视图控制器中,您需要设置变量。
// passed packName from PackViewController
var selectedPack: String!

然后在视图控制器中选择单元格

public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        // handle the segue to JourneyViewController with variable "selectedPack"
        // not sure why you need to set the storyboard but it works
    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        //create instance of the viewcontroller
    let transportJourneyViewController = storyBoard.instantiateViewController(withIdentifier: "JourneyViewController") as! JourneyViewController
        //value to pass - has been defined in journey
        transportJourneyViewController.selectedPack = INSERT_YOUR_VALUE_TO_PASS
        //present the vc
    self.present(transportJourneyViewController, animated:true, completion:nil)

}

JourneyViewController是你想要跳转到的视图控制器的StoryboardID和类名,在界面构建器中设置。
此外,你还需要在视图控制器的顶层和Storyboard本身中定义tableviewdatasource和tableviewdelegate。
class JourneyViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

我主要是想解决与tableview相关的问题。不过,使用UICollectionView听起来确实很有趣。如果我无法通过tableview解决问题,我会尝试使用UICollectionView。谢谢! - Mike Vaugh
只需将 "func collectionView(_ collectionView: UICollectionView" 更改为 "func tableView(_ tableView: UITableView" 即可。 - Pippo
显然,委托必须从“UICollectionViewDataSource,UICollectionViewDelegate”更改为“UITableViewDataSource,UITableViewDelegate”。 - Pippo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接