How to transfer data from one view controller to another SWIFT
I am creating an application where a table view with a search bar and a visibility bar needs to navigate to a detail view controller, and a detail view controller needs to display data based on a selected cell. I have an array with structures set up to sort and find items. I need to keep this function, I have another quick class for my detail view controller in which I will include if / else statements in this deal with data display based on which cell I select. I need to know how to do this: attach a variable to the cells and pass that variable to the detail view controller for use in my if / else statements so that I can display the data. If this is not the correct way to do it please let me know.
Structure code
struct Booth {
let category : String
let name : String
}
Table 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
}
}
}
}
source to share