2017-05-16 59 views
1

我試圖將值從一個視圖控制器傳遞到另一個視圖控制器。正如我測試這是好的,並與我的代碼運行。但是當我嘗試採取細胞標記價值時,我遇到了問題。我嘗試了一切,我發現谷歌和stackoverflow,但沒有答案。我在代碼中添加了一條評論,其中說明// I THINK THE PROBLEM IS ON THE NEXT LINE OF CODE可幫助您找到卡住的位置。無法從UITableViewCell獲取標籤值

我的代碼:

import UIKit 


class PlacesTableViewController: UITableViewController { 

@IBOutlet weak var placesTableView: UITableView! 

var places = [Places]() 
var valueToPass:String! 
let cellIdentifier = "PlacesTableViewCell" 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 
     // Table view cells are reused and should be dequeued using a cell identifier. 


     guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? PlacesTableViewCell else { 
      fatalError("The dequeued cell is not an instance of PlacesTableView Cell.") 
     } 

     let place = places[indexPath.row] 

     cell.placeLabel.text = place.name 
     cell.ratingControl.rating = place.rating 

     return cell 

    } 

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
    { 
     print("You selected cell #\(indexPath.item)!") 


     let indexPath = placesTableView.indexPathForSelectedRow 

     // I THINK THE PROBLEM IS ON THE NEXT LINE OF CODE 
     let currentCell = placesTableView.cellForRow(at: indexPath!)! as UITableViewCell 

     print(currentCell.detailTextLabel?.text ?? "Failed to use Cell Label") 
     valueToPass = currentCell.detailTextLabel?.text ?? "Failed to use Cell Label" 
     performSegue(withIdentifier: "ShowCommentsTableViewController", sender: self) 

    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if (segue.identifier == "ShowCommentsTableViewController") { 
      // initialize new view controller and cast it as your view controller 
      let viewController = segue.destination as! CommentsTableViewController 
      // will be stored in passedValue on the CommentsTableViewController 
      viewController.passedValue = valueToPass 
      //print (viewController.passedValue ?? "") 
     } 
    } 

} 
+0

使用dataSource,而不是單元格。此外,您正在使用'PlacesTableViewCell',而不是傳統的'UITableViewCell',並且'placeLabel'爲'UILabel',而不是'detailTextLabel'。 – Larme

+0

您應該只使用'places'數組來獲取數據,您已經在didSelect方法中使用了indexPath。 – Aks

+0

提示#1:遵循Scriptable的答案......提示#2:不要*「嘗試你在google和stackoverflow上找到的所有內容」* - **閱讀並理解你找到的內容,然後實現它。如果你不理解它,那麼在複製/粘貼的代碼中拍掌不會讓你到任何地方。 – DonMag

回答

3

你爲什麼不只是使用原始值從數據存儲就像你在cellForRowAt嗎?

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    let place = places[indexPath.row] 
    valueToPass = place.name 
    performSegue(withIdentifier: "ShowCommentsTableViewController", sender: self) 
} 
+0

建議編輯我的帖子將其更改爲'places [(indexPath?.row)!]'我拒絕了。 indexPath不是可選 – Scriptable

+0

但是我的編譯器說要改變它,因爲它無法構建。在我做出改變後,代碼正在運行,它發生了我想要的。謝謝您的回答。 –

+0

好吧,我會仔細檢查它並根據需要進行更新。上面的函數定義不顯示可選。更新:在xcode中對我來說很好 – Scriptable

0

有兩個基本的問題,在當前的代碼中,首先你是鑄造你的電池爲UITableViewCell,而不是PlacesTableViewCell和第二個問題是,你正在訪問detailTextLabel這是不是可以在您的自定義單元格,而不是使用placeLabel的。正確的代碼將是這樣的:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 

{ 
    print("You selected cell #\(indexPath.item)!") 


    let indexPath = placesTableView.indexPathForSelectedRow 

    // I THINK THE PROBLEM IS ON THE NEXT LINE OF CODE 
    let currentCell = placesTableView.cellForRow(at: indexPath!)! as PlacesTableViewCell 

    print(currentCell. placeLabel?.text ?? "Failed to use Cell Label") 
    valueToPass = currentCell. placeLabel?.text ?? "Failed to use Cell Label" 
    performSegue(withIdentifier: "ShowCommentsTableViewController", sender: self) 

} 

如果你不想去通過這個麻煩,你可以簡單地從原始數組一樣,得到的值:

let place = places[indexPath.row] 
valueToPass = place.name 
+0

任何人都會喜歡告訴我爲什麼有負面投票給我的答案? –

1

更換你didselect方法與下面給出的

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{ 

    let place = places[indexPath.row] 

    valueToPass = place.name 
    performSegue(withIdentifier: "ShowCommentsTableViewController", sender: self) 

} 
+0

謝謝!但@Scriptable是第一個。 –