2016-12-25 101 views
3

我目前正在学习Swift,并尝试在用户点击应用程序呈现的其中一个tableview单元格时执行segue。目前,无论用户何时执行此操作,下一个视图控制器都会成功加载,但似乎出于某种原因,我无法访问其任何UI元素,因为每次我尝试这样做时,我都会结束收到此错误:在tableview单元格上执行segue时出现问题

致命错误:意外发现零而展开的可选值

错误指向,我尝试修改显示在一个标签的文本行下一个视图控制器

这是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 

    } 
} 

我不知道是什么原因造成这个问题的想法。我的目标是将数据从窃听单元传递到下一个视图控制器,但显然这阻止了我这样做。有谁知道我该如何解决这个问题?提前致谢。

+0

嗨,迈克。你在使用静态或动态的'UITableView'单元吗? –

+0

我正在使用动态单元格。 –

+0

@Mike Vaugh,使用'destinationVC.usernameLabel.text = selectedCell.userName.text ?? “”' – aircraft

回答

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:)中使用的网点没有分配有关。

+0

Mate,you修好了!现在工作很好,谢谢,我很快就会接受你的回答 –

+0

太棒了!祝你好运!@MikeVaugh –

0

我在用UICollectionView尝试同样的事情时准备了用于segue方法的问题。 2非常相似,所以你应该可以轻松地将collectionview更改为tableview。

这是我做过什么......使用可变selectedPack

在视图控制器,你要原因请看你需要设置变量

// 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是你想要去to.set在Interface Builder中的ViewController的storyboardID和类名。

您还需要在视图控制器的顶层和故事板本身中定义tableviewdatasource和tableviewdelegate。

class JourneyViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 
+0

我主要是想用tableview来解决问题,特别是。但是,使用UICollectionView的确听起来很有趣。如果我不能用tableview来做,我会尝试一下。谢谢! –

+0

只需将“func collectionView(_ collectionView:UICollectionView”更改为“func tableView(_ tableView:UITableView”应该可以工作 – Pippo

+0

,而且显然这些委托必须从“UICollectionViewDataSource,UICollectionViewDelegate”更改为“UITableViewDataSource,UITableViewDelegate” – Pippo

相关问题