gpt4 book ai didi

swift - iOS8中如何实现UISearchController?

转载 作者:行者123 更新时间:2023-11-30 10:14:23 24 4
gpt4 key购买 nike

我尝试在 IOS8 中实现 UISearchController 但失败了。

问题是当我更改文本和范围按钮时,会出现注释。

当我更新搜索栏或范围按钮时,似乎甚至没有调用 updateSearchResultsForSearchController 函数。

这是我的代码:

class SearchTestController: UITableViewController, UISearchResultsUpdating {

struct Candy {
let category : String
let name : String
}

var searchcontroller = UISearchController(searchResultsController: nil)




func updateSearchResultsForSearchController(searchController: UISearchController) {
filteredcandy = candies.filter() { (candy:Candy) -> Bool in
let scopetest = ( self.category[self.searchcontroller.searchBar.selectedScopeButtonIndex] == "All" ) || ( candy.category == self.category[self.searchcontroller.searchBar.selectedScopeButtonIndex] )
//let texttest = candy.name.rangeOfString(self.searchcontroller.searchBar.text)
//let result = scopetest && (texttest != nil)
return scopetest
}
println(filteredcandy.count)
self.tableView.reloadData()
}


var candies = [Candy]()
var filteredcandy = [Candy]()
var category = ["Chocolate","Hard","Other","All"]


override func viewDidLoad() {
super.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()
self.tableView.tableHeaderView = searchcontroller.searchBar
searchcontroller.searchBar.sizeToFit()
searchcontroller.searchBar.showsSearchResultsButton = true
self.definesPresentationContext = true
searchcontroller.searchBar.scopeButtonTitles = category

}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchcontroller.active {
return self.candies.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("Cell") 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 searchcontroller.active {
candy = filteredcandy[indexPath.row]
} else {
candy = candies[indexPath.row]
}


// Configure the cell
cell.textLabel!.text = candy.name
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

return cell
}

最佳答案

将以下行添加到viewDidLoad()

searchcontroller.searchResultsUpdater = self
searchcontroller.delegate = self

更新:

viewDidLoad()中添加以下行

searchcontroller.searchBar.delegate = self

然后更新 searchBar(_:selectedScopeButtonIndexDidChange:) 中的搜索结果

关于swift - iOS8中如何实现UISearchController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31040314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com