I am making an app where a table view with a search bar and scope bar has to segue to a detail view controller and that detail view controller has to display data based on which cell is selected. I have an array with structs set up to sort and search for the items. I need to keep this feature, I have another swift class for my detail view controller that I will put if/else statements into that deal with displaying the data based on what cell I select. What I need to know how to do is attach a variable to the cells and pass that variable to the detail view controller to use in my if/else statements so that I can display the data. If this is not the correct way to do this, please let me know.
Struct code
struct Booth {
let category : String
let name : String
}
Table view controller code
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 code
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
}
}
}
}
For instance, in your BoothsTableViewController
create variable holding the currently selected Booth, which you assign in didSelectRowAtIndexPath
. You can then pass this variable to the detail view controller in your prepareForSegue
method.
Create a member variable/object in detailViewController. Set this object in prepareforSegue in your current TableViewController. This way the detailViewController will have the cell object the user clicked.
来源:https://stackoverflow.com/questions/26662256/how-to-pass-data-from-one-view-controller-to-the-other-swift