我试图在下面给出的表格视图中使用搜索显示功能在swift中实现搜索栏。执行搜索栏和搜索显示的表格视图与部分
截图表视图
的我得到的问题,因为我的字符串数组是按以下格式。
["section name1" : ["list1", "list2"] ,"section name2" : ["list3", "list4"]]
我在表格视图下面添加了我的代码。
@IBOutlet weak var dishtable: UITableView!
@IBOutlet weak var namlbl: UILabel!
var Dishes = ["POPULAR Dishes": ["Biryani", "Tandori Chicken","Butter Chicken", "Vada Pav"],"A": ["Aloo baingan", "Aloo ki Tikki", "Amritsari fish"], "B": ["Baigan bharta", "Biryani"]];
var Filterval = ["POPULAR Dishes": ["Biryani", "Tandori Chicken","Butter Chicken", "Vada Pav"],"A": ["Aloo baingan", "Aloo ki Tikki", "Amritsari fish"], "B": ["Baigan bharta", "Biryani"]];
struct dish {
let Dish : [String]
}
override func viewDidLoad() {
super.viewDidLoad()
self.dishtable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
dishtable.dataSource = self
dishtable.delegate = self
// Do any additional setup after loading the view.
}
func filterContentForSearchText(searchText: String) {
// Filter the array using the filter method
self.Filterval = self.Dishes.filter({(d: dish) -> Bool in
let DishMatch = (d.Dish == scope)
let stringMatch = d.Dish.rangeOfString(searchText)
return DishMatch && (stringMatch != nil)
})
}
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
self.filterContentForSearchText(searchString)
return true
}
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchScope searchOption: Int) -> Bool {
self.filterContentForSearchText(self.searchDisplayController!.searchBar.text!)
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
let sections:Array<AnyObject> = ["POPULAR Dishes","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
var usernames = [String]()
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cellID = "cell"
let cell: UITableViewCell = self.dishtable.dequeueReusableCellWithIdentifier(cellID) as! UITableViewCell
println("value : \(indexPath.section)")
println("value 1: \(indexPath.row)")
var d : dish
if tableView == self.searchDisplayController!.searchResultsTableView {
cell.textLabel!.text = Filterval[sections[indexPath.section] as! String]![indexPath.row]
} else {
cell.textLabel!.text = Dishes[sections[indexPath.section] as! String]![indexPath.row]
}
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
println("Dishes section count : \(section)")
if tableView == self.searchDisplayController!.searchResultsTableView {
if section == 0 {
return Filterval["POPULAR Dishes"]!.count
}
else if section == 1 {
return Filterval["A"]!.count
}
else if section == 2 {
return Filterval["B"]!.count
}
else if section == 3 {
return Filterval["C"]!.count
}
else if section == 4 {
return Filterval["D"]!.count
}
else if section == 5 {
return Filterval["E"]!.count
}
else if section == 6 {
return Filterval["F"]!.count
}
return 0
} else {
if section == 0 {
return Dishes["POPULAR Dishes"]!.count
}
else if section == 1 {
return Dishes["A"]!.count
}
else if section == 2 {
return Dishes["B"]!.count
}
else if section == 3 {
return Dishes["C"]!.count
}
else if section == 4 {
return Dishes["D"]!.count
}
else if section == 5 {
return Dishes["E"]!.count
}
else if section == 6 {
return Dishes["F"]!.count
}
return 0
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int{
return 27
}
func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
}
func tableView(tableView: UITableView,
sectionForSectionIndexTitle title: String,
atIndex index: Int) -> Int{
return index
}
func tableView(tableView: UITableView,
titleForHeaderInSection section: Int) -> String?{
return self.sections[section] as? String
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
tableView.deselectRowAtIndexPath(indexPath, animated: true)
var ban = ""
ban = Dishes[sections[indexPath.section] as! String]![indexPath.row]
println(ban)
}
我试过以下链接在线,但无法实现搜索功能。
http://www.ioscreator.com/tutorials/add-search-table-view-tutorial-ios8-swift
http://www.raywenderlich.com/76519/add-table-view-search-swift
有人可以帮我搜索栏?
你能解释一下关于什么是确切的问题吗?搜索结果不是你期望的,它会崩溃吗? – Chackle
其实在搜索结果中根本不起作用。我正在整理所有清单。 – Karlos
我没有看到任何引用搜索视图的代码。你有没有实现与它有关的任何事情? – Chackle