我已经查看了一些searchBar的示例代码并将其关注到了tee,但我的searchBar无效。我有一个Master-Detail应用程序,搜索在Master上,它有一个searchBar和一个UITableView。每当我输入任何内容时,我希望tableview减少显示。例如,当我输入“G”时,它应显示“非洲菊”或任何以“G”开头的花。
enter code here
//
// MasterViewController.swift
// SplitView3
//
// Created by Sheryll See on 2017-09-28.
// Copyright © 2017 Sheryll See. All rights reserved.
//
import UIKit
public class MasterViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate {
@IBOutlet weak var searchBar: UISearchBar!
var detailViewController: DetailViewController? = nil
var tableView: UITableView? = nil
var objects = [Any]()
var siteAddressesRed: [String]?
var siteAddressesBlue: [String]?
var siteDetailRed: [String]?
var siteDetailBlue: [String]?
let kSectionCount:Int = 2
let kRedSection: Int = 0
let kBlueSection: Int = 1
let redFlowers: [String] = ["Gerbera", "Peony", "Rose", "Poppy"]
let blueFlowers: [String] = ["Hyacinth", "Hydrangea", "Sea Holly", "Phlox", "Iris"]
let allFlowers: [String] = ["Gerbera", "Peony", "Rose", "Poppy", "Hyacinth", "Hydrangea", "Sea Holly", "Phlox", "Iris"]
var filteredData = [String]()
var isSearching = false
struct MyVariables {
static var urlString: [String]? = ["something"]
static var flowerImage: UIImage?
}
override public func viewDidLoad() {
super.viewDidLoad()
siteAddressesRed = ["https://en.wikipedia.org/wiki/Gerbera",
"https://en.wikipedia.org/wiki/Peony",
"https://en.wikipedia.org/wiki/Rose",
"https://en.wikipedia.org/wiki/Poppy"]
siteAddressesBlue = ["https://en.wikipedia.org/wiki/Hyacinth",
"https://en.wikipedia.org/wiki/Hydrangea",
"https://en.wikipedia.org/wiki/Sea_Holly",
"https://en.wikipedia.org/wiki/Phlox",
"https://en.wikipedia.org/wiki/Iris"]
siteDetailRed = ["Coquitlam1",
"Coquitlam2",
"Coquitlam3",
"Coquitlam4"]
siteDetailBlue = ["Coquitlam1",
"Coquitlam2",
"Coquitlam3",
"Coquitlam4",
"Coquitlam5"]
searchBar.delegate = self
searchBar.returnKeyType = UIReturnKeyType.done
filteredData = allFlowers
if let split = splitViewController {
let controllers = split.viewControllers
self.detailViewController = (controllers[controllers.count-1] as!
UINavigationController).topViewController
as? DetailViewController
}
}
override public func viewWillAppear(_ animated: Bool) {
//clearsSelectionOnViewWillAppear = splitViewController!.isCollapsed
super.viewWillAppear(animated)
}
override public func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@objc
func insertNewObject(_ sender: Any) {
objects.insert(NSDate(), at: 0)
let indexPath = IndexPath(row: 0, section: 0)
tableView?.insertRows(at: [indexPath], with: .automatic)
//tableView.insertRows(at: [indexPath], with: .automatic)
}
// MARK: - Segues
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath:IndexPath) {
if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
switch indexPath.section {
case 0:
do {
MyVariables.urlString = [siteAddressesRed![indexPath.row]]
MyVariables.flowerImage = UIImage(named: redFlowers[indexPath.row])
// MyVariables.flowerImage = [UIImage(named: "Rose.png")!]
print("Im here")
}
case 1:
do {
MyVariables.urlString = [siteAddressesBlue![indexPath.row]]
MyVariables.flowerImage = UIImage(named: blueFlowers[indexPath.row])
print("Im there")
}
default:
do {
MyVariables.urlString = ["https://en.wikipedia.org/flowers"]
MyVariables.flowerImage = UIImage(named: redFlowers[indexPath.row])
}
}
}
}
override public func prepare(for segue: UIStoryboardSegue,
sender: Any?)
{
if segue.identifier == "showDetail" {
if let indexPath = self.tableView?.indexPathForSelectedRow {
switch indexPath.section {
case 0:
do {
MyVariables.urlString = [siteAddressesRed![indexPath.row]]
MyVariables.flowerImage = UIImage(named: redFlowers[indexPath.row])
// MyVariables.flowerImage = [UIImage(named: "Rose.png")!]
}
case 1:
do {
MyVariables.urlString = [siteAddressesBlue![indexPath.row]]
MyVariables.flowerImage = UIImage(named: blueFlowers[indexPath.row])
}
default:
do {
MyVariables.urlString = ["https://en.wikipedia.org/flowers"]
MyVariables.flowerImage = UIImage(named: redFlowers[indexPath.row])
}
}
/*
let controller = (segue.destination as! UINavigationController).topViewController
as! DetailViewController
controller.detailItem = MyVariables.urlString as [String]! as AnyObject
controller.navigationItem.leftBarButtonItem =
splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton
= true */
}
let controller = (segue.destination as! UINavigationController).topViewController
as! DetailViewController
controller.detailItem = MyVariables.urlString as [String]! as AnyObject
controller.navigationItem.leftBarButtonItem =
splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton
= true
}
}
// MARK: - Table View
public func numberOfSections(in tableView: UITableView) -> Int
{
return kSectionCount
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
switch section {
case kRedSection:
return redFlowers.count
return filteredData.count
case kBlueSection:
return blueFlowers.count
return filteredData.count
default:
return 0
return filteredData.count
}
if isSearching {
return filteredData.count
}
return filteredData.count
}
public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch section {
case kRedSection:
return "Red"
case kBlueSection:
return "Blue"
default:
return "Unknown"
}
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",
for: indexPath)
//cell.textLabel!.text = siteNames![indexPath.row]
switch (indexPath.section) {
case kRedSection:
do { cell.textLabel!.text = redFlowers[indexPath.row]
cell.detailTextLabel!.text = siteDetailRed![indexPath.row]
if isSearching {
cell.textLabel!.text = filteredData[indexPath.row]
}
else {
cell.textLabel!.text = redFlowers[indexPath.row]
}
}
case kBlueSection:
do {
cell.textLabel!.text = blueFlowers[indexPath.row]
cell.detailTextLabel!.text = siteDetailBlue![indexPath.row]
if isSearching {
cell.textLabel!.text = filteredData[indexPath.row]
}
else {
cell.textLabel!.text = blueFlowers[indexPath.row]
}
}
default:
do {
cell.textLabel!.text = "Unknown"
cell.detailTextLabel!.text = "Unknown"
if isSearching {
cell.textLabel!.text = filteredData[indexPath.row]
}
else {
cell.textLabel!.text = redFlowers[indexPath.row]
}
}
}
cell.textLabel?.text = filteredData[indexPath.row]
let test = UIImage(named: cell.textLabel!.text!)!
cell.imageView!.image = test
MyVariables.flowerImage = test
return cell
}
public func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
objects.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
public func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
// When there is no text, filteredData is the same as the original data
// When user has entered text into the search box
// Use the filter method to iterate over all items in the data array
// For each item, return true if the item should be included and false if the
// item should NOT be included
filteredData = searchText.isEmpty ? allFlowers : allFlowers.filter({(dataString: String) -> Bool in
// If dataItem matches the searchText, return true to include it
return dataString.range(of: searchText, options: .caseInsensitive) != nil
})
tableView?.reloadData()
} }
答案 0 :(得分:0)
我找到了答案。我没有tableView的IBOutlet,我不得不删除searchBar IBOutlet并将其重新绑定到searchBar。