2014-10-30 42 views
0

我正在嘗試使用基於哪個表格單元格被選中的信息填充詳細視圖控制器。我試圖做什麼,我認爲可能的工作,但我得到的錯誤「使用未解決的標識符‘細節’的上說行,如果(詳細)在詳細視圖控制器類。在swift中使用交叉類變量

@IBOutlet var detailText: UILabel! 



override func viewDidLoad() { 
    super.viewDidLoad() 

    if (detail = "conferenceapp") { 
    self.detailText.text = "lol" 
    } 

    if (detail = "spaceshooter") { 
     self.detailText.text = "spaceshooter" 
    } 

展位結構

struct Booth { 
let category : String 
let name : String 
let detail : String 

}

表視圖控制器類代碼段

var booths = [Booth]() 
var filteredBooths = [Booth]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    //fill array with data 
    self.booths = [Booth(category: "Tech", name: "Conference App", detail: "conferenceapp"), 
     Booth(category: "Tech", name: "Space Shooter", detail: "spaceshooter"), 
     Booth(category: "Tech", name: "RollABall", detail: "rollaball"), 
     Booth(category: "Animation", name: "Sugar Hill City Model", detail: "sugar"), 
     Booth(category: "Animation", name: "3D Sculpting 101", detail: "3d"), 
     Booth(category: "Animation", name: "HowTo - Texture", detail: "howto"), 
     Booth(category: "Science", name: "AP Biology for Dummies", detail: "apbio"), 
     Booth(category: "Science", name: "Cells", detail: "cells"), 
     Booth(category: "Science", name: "Space", detail: "space")] 

PL緩解幫助,我需要修復錯誤

回答

1

將數據傳遞到詳細視圖的常用方法是通過prepareForSegue函數。例如:

import UIKit 

class MasterViewController: UITableViewController { 

    let items = ["Bob","Joe"] 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return items.count 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Item", forIndexPath: indexPath) as UITableViewCell 

     cell.textLabel.text = items[indexPath.row] 

     return cell 
    } 

//Passing details to detail VC 
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

      let indexPath = self.tableView.indexPathForSelectedRow() 
      let theDestination = (segue.destinationViewController as DetailViewController) 
      theDestination.itemName = items[indexPath!.row] 
    } 

} 

import UIKit 

class DetailViewController: UIViewController { 

    @IBOutlet weak var nameLabel: UILabel! 

    var itemName = "" 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     nameLabel.text = itemName 
    } 

} 
+0

我想在展臺結構中設置等於如self.booths = [展位(類別:「技術」,名稱:「會議應用程序」,細節:「conferenceapp」)的字符串中的細節, – user3558131 2014-10-30 01:47:23

+0

在細節類中,我必須檢查「細節」是否等於它在這些「展位(類別:」技術「,名稱:」太空射手「,細節:」太空射手「)中設置的任何字符串, 展位(類別:「Tech」,名稱:「RollABall」,詳細信息:「rollaball」), 展位(類別:「動畫」,名稱:「Sugar Hill City Model」,細節:「糖」), 展位「動畫」,名稱:「3D雕刻101」,詳細信息:「3d」),' – user3558131 2014-10-30 01:49:44

+1

您仍按照我向您展示的方式進行。我假設表的每一行都從數組中獲取一個元素?如果是這樣你在我的示例 – 2014-10-30 01:52:21

1

的問題是,你說的if (detail = "conference app")但沒有定義什麼叫「細節」。 「細節」變量應該來自哪裏?您是否想參考展位的detail?然後你需要一個展位,你需要指定的細節。但是,您沒有展位,無論如何只是將「細節」作爲一個裸露的單詞,並不涉及的細節。

另外我想知道你的意思是if (detail == "conference app")。你不能像你一樣在Swift中使用單個等號。

+0

我試圖訪問展臺結構中的「細節」結構 – user3558131 2014-10-30 01:41:15

+0

在其中一個這些展位(類別:「Tech」,名稱:「Space Shooter」,詳細信息:「spaceshooter」), – user3558131 2014-10-30 01:41:39

+1

但是,正如我所說的那樣,假設您有一個_reference to_其中一個Booth對象。你沒有。即使你做了,你也不能只說'細節'。你需要說'someBooth.detail'(如果「someBooth」是你的Booth對象的名字)。 – matt 2014-10-30 02:24:04