我实现了带有搜索控件的表格视图。下面的代码总结了这一切。但是它会引发警告:在事务内调用[CATransaction syncize]
我不确定它的含义以及它对我的应用有多严重的影响。
如果这是有害的,您能否根据我的代码帮助我消除此错误:
import UIKit
class CountryTableViewController: UITableViewController, UISearchResultsUpdating {
let countriesArray = ["Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria"]
let countrycodes = ["+93", "+355", "+213", "+93", "+355", "+213", "+93", "+355", "+213", "+93", "+355", "+213", "+93", "+355", "+213", "+93", "+355", "+213", "+93", "+355", "+213"]
var filteredArray = [String]()
var searchController = UISearchController()
var resultsController = UITableViewController()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let cancelButton: UIBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancel))
self.navigationItem.leftBarButtonItem = cancelButton
let searchButton: UIBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: self, action: #selector(searchButtonAction))
searchButton.image = UIImage(named: "search")
self.navigationItem.rightBarButtonItem = searchButton
searchController = UISearchController(searchResultsController: resultsController)
searchController.searchResultsUpdater = self
resultsController.tableView.delegate = self
resultsController.tableView.dataSource = self
definesPresentationContext = true
}
func updateSearchResults(for searchController: UISearchController)
{
filteredArray = countriesArray.filter({ (countriesArray:String) -> Bool in
if countriesArray.contains(searchController.searchBar.text!)
{
return true
}
else
{
return false;
}
})
resultsController.tableView.reloadData()
}
@objc func cancel() {
self.dismiss(animated: true, completion: nil)
}
@objc func searchButtonAction() {
self.dismiss(animated: true, completion: nil)
tableView.tableHeaderView = searchController.searchBar
self.searchController.isActive = true
self.searchController.searchBar.text = ""
self.navigationItem.rightBarButtonItem = nil
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == resultsController.tableView
{
return filteredArray.count
}
else
{
return countriesArray.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell()
if tableView == resultsController.tableView
{
cell.textLabel?.text = filteredArray[indexPath.row]
cell.detailTextLabel?.text = countrycodes[indexPath.row]
cell.imageView?.image = UIImage (named: "Flag0")
}
else
{
cell.textLabel?.text = countriesArray[indexPath.row]
cell.detailTextLabel?.text = countrycodes[indexPath.row]
cell.imageView?.image = UIImage (named: "Flag0")
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row, indexPath.section)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}