2014-10-30 183 views
3

我正在製作一個應用程序,其中帶有搜索欄和範圍欄的表視圖必須繼承到詳細視圖控制器,並且該詳細視圖控制器必須基於哪個單元格被選中。我有一個數組,其結構設置爲排序和搜索項目。我需要保持這個功能,我有另一個我的詳細信息視圖控制器的swift類,我將把if/else語句放入顯示基於我選擇的單元格的數據的處理中。我需要知道如何做的是將一個變量附加到單元格中,並將該變量傳遞給詳細視圖控制器以在我的if/else語句中使用,以便我可以顯示數據。如果這不是這樣做的正確方法,請讓我知道。如何將數據從一個視圖控制器傳遞到另一個SWIFT

結構代碼

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

表視圖控制器代碼

import UIKit 

class BoothsTableViewController: UITableViewController {  

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

override func viewDidLoad() { 
    super.viewDidLoad() 

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

    //reload the table 
    self.tableView.reloadData() 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if tableView == self.searchDisplayController!.searchResultsTableView { 
     return self.filteredBooths.count 
    } else { 
     return self.booths.count 
    } 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    //ask for a reusable cell from the tableview, the tableview will create a new one if it doesn't have any 
    let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell 

    var boothsCheck : Booth 
    // Check to see whether the normal table or search results table is being displayed and set the Booth object from the appropriate array 
    if tableView == self.searchDisplayController!.searchResultsTableView { 
     boothsCheck = filteredBooths[indexPath.row] 
    } else { 
     boothsCheck = booths[indexPath.row] 
    } 

    // Configure the cell 
    cell.textLabel.text = boothsCheck.name 
    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator 

    return cell 
} 

func filterContentForSearchText(searchText: String, scope: String = "All") { 
    self.filteredBooths = self.booths.filter({(Booth : Booth) -> Bool in 
     var categoryMatch = (scope == "All") || (Booth.category == scope) 
     var stringMatch = Booth.name.rangeOfString(searchText) 
     return categoryMatch && (stringMatch != nil) 
    }) 
} 

func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool { 
    let scopes = self.searchDisplayController!.searchBar.scopeButtonTitles as [String] 
    let selectedScope = scopes[self.searchDisplayController!.searchBar.selectedScopeButtonIndex] as String 
    self.filterContentForSearchText(searchString, scope: selectedScope) 
    return true 
} 

func searchDisplayController(controller: UISearchDisplayController!, 
    shouldReloadTableForSearchScope searchOption: Int) -> Bool { 
     let scope = self.searchDisplayController!.searchBar.scopeButtonTitles as [String] 
     self.filterContentForSearchText(self.searchDisplayController!.searchBar.text, scope: scope[searchOption]) 
     return true 
} 

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    self.performSegueWithIdentifier("BoothDetail", sender: tableView) 
} 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

    if segue.identifier == "BoothDetail" { 

     let BoothDetailViewController = segue.destinationViewController as UIViewController 


     if sender as UITableView == self.searchDisplayController!.searchResultsTableView { 
      let indexPath = self.searchDisplayController!.searchResultsTableView.indexPathForSelectedRow()! 
      let destinationTitle = self.filteredBooths[indexPath.row].name 
      BoothDetailViewController.title = destinationTitle 


    } else { 

      let indexPath = self.tableView.indexPathForSelectedRow()! 
      let destinationTitle = self.booths[indexPath.row].name 
      BoothDetailViewController.title = destinationTitle 
      } 
     } 
    } 
} 

Segue公司代碼

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

    if segue.identifier == "BoothDetail" { 

     let BoothDetailViewController = segue.destinationViewController as UIViewController 


     if sender as UITableView == self.searchDisplayController!.searchResultsTableView { 
       let indexPath = self.searchDisplayController!.searchResultsTableView.indexPathForSelectedRow()! 
       let destinationTitle = self.filteredBooths[indexPath.row].name 
       BoothDetailViewController.title = destinationTitle   
     } else {    
       let indexPath = self.tableView.indexPathForSelectedRow()! 
       let destinationTitle = self.booths[indexPath.row].name 
       BoothDetailViewController.title = destinationTitle 
      } 
     } 
    } 
} 

回答

3

例如,在你的BoothsTableViewController創建變量保存當前選定展位,您指定在didSelectRowAtIndexPath。然後,您可以將此變量傳遞給您的prepareForSegue方法中的詳細視圖控制器。

+0

是的,謝謝,事情是我不知道如何傳遞它@Rick – user3558131 2014-10-30 20:25:58

+0

只需在詳細視圖中創建一個變量,您可以在創建'DetailViewController'後設置變量,就像你一樣與標題做。 – Rick 2014-10-30 20:27:08

0

在detailViewController中創建成員變量/對象。在您當前的TableViewController中的prepareforSegue中設置此對象。這樣detailViewController將擁有用戶點擊的單元格對象。

相關問題