Hi,
I have been looking for an example of how to create a grouped TableView in Swift but have had no luck.. I am working on a project to create a Categories Page and currently have a working search bar as well as the sub-categories listed in a TableView.. I'd like to be able to break these categories into a grouped TableView so that it is easier for the user.. Can anyone help me find a good example which can guide me? Or provide any advice? I was thinking since in my array I already have a category defined for each.. that I would try and break it up by Travel, Wireless Phones, and Entertainment?
I'm pretty new to this so anything helps!
Here is what I have in my Category.swift file:
import Foundation
struct Category {
let category : String
let name : String
}
And here is what I have so far for my CategoryTableViewController:
import UIKit
class CategoryTableViewController : UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate {
var categories = [Category]()
var filteredCategories = [Category]()
override func viewDidLoad() {
self.categories = [Category(category:"Travel", name:"Rental Cars"),
Category(category:"Travel", name:"Hotels"),
Category(category:"Travel", name:"Cruises and Tours"),
Category(category:"Entertainment", name:"Amusement Parks"),
Category(category:"Entertainment", name:"Movie Tickets"),
Category(category:"Entertainment", name:"Sports Tickets"),
Category(category:"Cell Phones", name:"Accessories"),
Category(category:"Cell Phones", name:"Wireless Carriers"),]
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.filteredCategories.count
} else {
return self.categories.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
var category : Category
if tableView == self.searchDisplayController!.searchResultsTableView {
category = filteredCategories[indexPath.row]
} else {
category = categories[indexPath.row]
}
cell.textLabel!.text = category.name
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell
}
func filterContentForSearchText(searchText: String, scope: String = "All") {
self.filteredCategories = self.categories.filter({( category : Category) -> Bool in
var categoryMatch = (scope == "All") || (category.category == scope)
var stringMatch = category.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("categoryDetail", sender: tableView)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "categoryDetail" {
let categoryDetailViewController = segue.destinationViewController as UIViewController
if sender as UITableView == self.searchDisplayController!.searchResultsTableView {
let indexPath = self.searchDisplayController!.searchResultsTableView.indexPathForSelectedRow()!
let destinationTitle = self.filteredCategories[indexPath.row].name
categoryDetailViewController.title = destinationTitle
} else {
let indexPath = self.tableView.indexPathForSelectedRow()!
let destinationTitle = self.categories[indexPath.row].name
categoryDetailViewController.title = destinationTitle
}
}
}
}