2016-12-25 95 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

相關問題