所以我正在尝试实现一个TableView搜索栏,我已经设置并处理了搜索栏的viewcontroller,但是在查看详细视图时遇到了麻烦(即搜索结果表中单击的单元格)。我正在使用本教程,该教程从scratch swift文件开始,并将tableView Search作为初始ViewController。重申一下,tableView搜索栏位于其他tableView之间,并不是最初的VC(希望我能正确解释。我注意到“Show”segue不是从搜索表到详细ViewController的选项。这是代码......还附有故事板的截图
非常感谢!
// InitialViewController.swift
import UIKit
class ViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var myTableView: UITableView!
var arrayOfPersons: [Person] = [Person]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.setUpPersons()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setUpPersons()
{
var person1 = Person(name: "Anthony Valentine", number: 60, imageName: "Anthony Valentine.png")
var person2 = Person(name: "Dan Meier", number: 10, imageName: "Dan Meier.png")
var person3 = Person(name: "Mark Druffel", number: 10, imageName: "Dan Meier.png")
var person4 = Person(name: "Brittany Donnelly", number: 10, imageName: "Dan Meier.png")
var person5 = Person(name: "David Moss", number: 10, imageName: "Dan Meier.png")
var person6 = Person(name: "Ben Matthews", number: 10, imageName: "Dan Meier.png")
var person7 = Person(name: "Alex Curtis", number: 10, imageName: "Dan Meier.png")
var person8 = Person(name: "Christine Meier", number: 10, imageName: "Dan Meier.png")
var person9 = Person(name: "Billy Kolesar", number: 10, imageName: "Dan Meier.png")
arrayOfPersons.append(person1)
arrayOfPersons.append(person2)
arrayOfPersons.append(person3)
arrayOfPersons.append(person4)
arrayOfPersons.append(person5)
arrayOfPersons.append(person6)
arrayOfPersons.append(person7)
arrayOfPersons.append(person8)
arrayOfPersons.append(person9)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayOfPersons.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("Cell") as CustomCell
let person = arrayOfPersons[indexPath.row]
cell.setCell(person.name, rightLabelInt: person.number, imageName: person.imageName)
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let person = arrayOfPersons[indexPath.row]
var detailedViewController: DetailViewController = self.storyboard?.instantiateViewControllerWithIdentifier("DetailViewController")! as DetailViewController
detailedViewController.nameString = person.name
detailedViewController.ageInt = person.number
detailedViewController.myDetailedImageName = person.imageName
self.presentViewController(detailedViewController, animated: true, completion: nil)
}
}
// Friends.swift
// TravelAppSwiftPrototype
//
// Created by Daniel on 10/23/14.
// Copyright (c) 2014 Dan Meier. All rights reserved.
//
import Foundation
struct Candy {
let category : String
let name : String
}
//
// SearchFriendsViewController.swift
import UIKit
class SearchFriendsViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate {
var candies = [Candy]()
var filteredCandies = [Candy]()
override func viewDidLoad() {
// Sample Data for candyArray
self.candies = [Candy(category:"Chocolate", name:"chocolate Bar"),
Candy(category:"Chocolate", name:"chocolate Chip"),
Candy(category:"Chocolate", name:"dark chocolate"),
Candy(category:"Hard", name:"lollipop"),
Candy(category:"Hard", name:"candy cane"),
Candy(category:"Hard", name:"jaw breaker"),
Candy(category:"Other", name:"caramel"),
Candy(category:"Other", name:"sour chew"),
Candy(category:"Other", name:"gummi bear")]
// Reload the table
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.filteredCandies.count
} else {
return self.candies.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("SearchFriendsCell") as UITableViewCell
var candy : Candy
// Check to see whether the normal table or search results table is being displayed and set the Candy object from the appropriate array
if tableView == self.searchDisplayController!.searchResultsTableView {
candy = filteredCandies[indexPath.row]
} else {
candy = candies[indexPath.row]
}
// Configure the cell
cell.textLabel.text = candy.name
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell
}
func filterContentForSearchText(searchText: String, scope: String = "All") {
self.filteredCandies = self.candies.filter({( candy : Candy) -> Bool in
var categoryMatch = (scope == "All") || (candy.category == scope)
var stringMatch = candy.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("FriendsDetail", sender: tableView)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "FriendsDetail" {
let candyDetailViewController = segue.destinationViewController as UIViewController
if sender as UITableView == self.searchDisplayController!.searchResultsTableView {
let indexPath = self.searchDisplayController!.searchResultsTableView.indexPathForSelectedRow()!
let destinationTitle = self.filteredCandies[indexPath.row].name
candyDetailViewController.title = destinationTitle
} else {
let indexPath = self.tableView.indexPathForSelectedRow()!
let destinationTitle = self.candies[indexPath.row].name
candyDetailViewController.title = destinationTitle
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}