在范围XCODE之间切换时保持搜索栏内容

时间:2018-06-10 17:03:06

标签: xcode uisearchbar searchbar

我创建了一个有两个可搜索选项的表。区域(使用搜索栏)和类型(使用范围)。

当我在搜索栏中输入一个区域时会弹出相关结果。但是,当我尝试使用范围添加类型时,它会完全忽略搜索栏内容。

我希望能够使用搜索栏进行搜索,然后使用范围来过滤搜索。

请建议。

代码

import UIKit

class LakeDirectoryViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate  {
    @IBOutlet var table: UITableView!
    @IBOutlet var searchBar: UISearchBar!

var fisheryArray = [Fishery]() // to set up table
var currentFisheryArray = [Fishery]()

override func viewDidLoad() {
    super.viewDidLoad()
    setUpFishery()
    setUpSearchBar()


}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

private func setUpFishery() {
    //Day Tickets
    fisheryArray.append(Fishery(title: "Wraysbury", region: "Surrey", category: .dayTicket, image:"1"))
    fisheryArray.append(Fishery(title: "Wraysbury", region: "Barkshire", category: .dayTicket, image:"2"))
    //Club Waters
    fisheryArray.append(Fishery(title: "Wraysbury", region: "Hampshire", category: .clubWater, image:"3"))
    fisheryArray.append(Fishery(title: "Wraysbury", region: "Derbyshire", category: .clubWater, image:"4"))
    //Syndicate
    fisheryArray.append(Fishery(title: "Wraysbury", region: "Derbyshire", category: .syndicate, image:"5"))
    fisheryArray.append(Fishery(title: "Wraysbury", region: "Surrey", category: .syndicate, image:"6"))

    currentFisheryArray = fisheryArray
}
private func setUpSearchBar() {
    searchBar.delegate = self

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return currentFisheryArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? TableCell else {
        return UITableViewCell()
    }
    cell.RegionLb1.text = currentFisheryArray[indexPath.row].region
    cell.TypeLb1.text = currentFisheryArray[indexPath.row].category.rawValue
    cell.Img1.image = UIImage(named:currentFisheryArray[indexPath.row].image)
    cell.TitleLb1.text = currentFisheryArray[indexPath.row].title
    return cell

}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100

}


// Search bar
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    currentFisheryArray = fisheryArray.filter({ fishery -> Bool in
        switch searchBar.selectedScopeButtonIndex {
        case 0:
            if searchText.isEmpty { return true }
            return fishery.region.lowercased().contains(searchText.lowercased())
        case 1:
            if searchText.isEmpty { return fishery.category == .dayTicket }
            return fishery.region.lowercased().contains(searchText.lowercased()) && fishery.category == .dayTicket
        case 2:
            if searchText.isEmpty { return fishery.category == .clubWater }
            return fishery.region.lowercased().contains(searchText.lowercased()) && fishery.category == .clubWater
        case 3:
            if searchText.isEmpty { return fishery.category == .syndicate }
            return fishery.region.lowercased().contains(searchText.lowercased()) && fishery.category == .syndicate
        default:
            return false
        }
    })
    table.reloadData()
}


func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
    switch selectedScope {
    case 0:
        currentFisheryArray = fisheryArray
    case 1:
        currentFisheryArray = fisheryArray.filter({ fishery -> Bool in
            fishery.category == FisheryType.dayTicket
        })
    case 2:
        currentFisheryArray = fisheryArray.filter({ fishery -> Bool in
            fishery.category == FisheryType.clubWater
        })
    case 3:
        currentFisheryArray = fisheryArray.filter({ fishery -> Bool in
            fishery.category == FisheryType.syndicate
        })
    default:
        break
    }
   table.reloadData()
    }

}
class Fishery {
    let title: String
    let region: String
    let image: String
    let category: FisheryType

    init(title: String, region: String, category: FisheryType, image: String) {
        self.title = title
        self.region = region
        self.category = category
        self.image = image
    }
}

enum FisheryType: String {
    case dayTicket = "Day Ticket"
    case clubWater = "Club Water"
    case syndicate = "Syndicate"
}

0 个答案:

没有答案