gpt4 book ai didi

ios - UISearchController 搜索栏动画第一次很慢

转载 作者:IT王子 更新时间:2023-10-29 05:32:17 24 4
gpt4 key购买 nike

在 iOS 9 中,我正在使用 UISearchController 并在 UIViewController 中显示其搜索栏,我第一次单击搜索栏时遇到了很多延迟,并且尝试了我能想到的所有方法都无济于事......下面是我的代码以及指向发生延迟的视频的链接 - 延迟发生在模拟器和我的设备上。

func setupUI() {
self.view.backgroundColor = UIColor.whiteColor()

// Required to properly display searchbar within nav & tabbar controllers
self.extendedLayoutIncludesOpaqueBars = true // have tried setting this to false as well
self.definesPresentationContext = true

self.searchResultsController = AppDelegate.getViewController(ScheduleStoryboard.name, controllerName: ScheduleStoryboard.Identifiers.foodSearchResults) as? SearchResultsController

self.searchController = UISearchController(searchResultsController: searchResultsController)
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.dimsBackgroundDuringPresentation = true

self.searchController.searchBar.delegate = self
self.searchController.searchBar.placeholder = "Search foods..."
self.searchController.searchBar.setBackgroundImage(UIImage(named: "background-searchbar")?.resizableImageWithCapInsets(UIEdgeInsetsMake(0, 0, 0, 0)), forBarPosition: .Any, barMetrics: .Default)
self.searchController.searchBar.tintColor = UIColor.whiteColor()
self.searchController.searchBar.sizeToFit()

// this headerView does NOT belong to the tableView, its anchored on top of the tableView so that the searchbar remains fixed when scrolling
self.headerView.addSubview(searchController.searchBar)

self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.tableHeaderView?.backgroundColor = UIColor.clearColor()
self.tableView.tableHeaderView?.addBorder(.Bottom, color: UIColor.groupTableViewBackgroundColor(), width: 0.25)

self.segmentedControl.tintColor = UIColor.genioBlue()
}

这是一个显示正在发生的事情的视频的链接:http://sendvid.com/xgq81stx

谢谢!

最佳答案

我只创建过一次搜索 Controller ,但我使用了 UITableViewController 作为我的基类。这是我的实现:

class SearchController: UITableViewController {
let searchController = UISearchController(searchResultsController: nil)
var items:[ArrayOfYourType]
var filteredItems:[ArrayOfYourType]
var scopeTitles:[String]?

override func viewDidLoad() {
super.viewDidLoad()

searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar

searchController.searchBar.scopeButtonTitles = scopeTitles
searchController.searchBar.delegate = self
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.active {
return filteredItems.count
}
return items.count
}

func filterContentForSearchText(searchText: String, scope: String = "All") {
filteredItems = items.filter { item in
//return true or false depending on your filter
return true
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
reuseIdentifier: nil)
let item: String
let category: String
if searchController.active {
item = filteredItems[indexPath.row].getTitle()
category = filteredItems[indexPath.row].getCategory()
}
else {
item = items[indexPath.row].getTitle()
category = items[indexPath.row].getCategory()
}
cell.textLabel?.text = item
cell.detailTextLabel?.text = category
return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//your code here
}
}

//MARK: UISearchResultsUpdating
extension SearchController: UISearchResultsUpdating {
func updateSearchResultsForSearchController(searchController: UISearchController) {
if let _ = scopeTitles {
let searchBar = searchController.searchBar
let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
filterContentForSearchText(searchController.searchBar.text!,scope:scope)
}
else {
filterContentForSearchText(searchController.searchBar.text!)
}
}
}

//MARK: UISearchBarDelegate
extension SearchController: UISearchBarDelegate {
func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
filterContentForSearchText(searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
}
}

希望对您有所帮助:)

关于ios - UISearchController 搜索栏动画第一次很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34347035/

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