//当我单击例如A的字母时,其数据来自服务器,但没有填写集合视图。当我单击字母 S 时,A 值资产正在填写集合 view.AS 当我从 AS 值中删除 A 时会显示值。//
@objc var filteredDataItems = Array((
@objc var searchCollectionList1 = NSMutableArray()
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
searchBar.barTintColor = UIColor.white
let lists:Array = searchCollectionList1 as Array
searchFilter(searchName: searchText)
filteredDataItems = lists
print(searchText)
self.collectionView.reloadData()
}
func updateSearchResults(for searchController: UISearchController)
{
searchController.searchBar.delegate = self
}
@objc func searchFilter(searchName:String)
{
self.searchCollectionList1.removeAllObjects()
let url = KsearchFilter + “appname=(kAppName)”
if self.debugPrint == true
{
print(“searchFilter--URL--SearchListViewController-----(url)-------------“)
}
let parameters = [“search”:searchName]
ApiManager.sharedManager.postDataWithJsonLambda(url: url, parameters: parameters)
{
(responseDict,error,isDone)in
// print(“responseDictresponseDict(String(describing: responseDict))“)
if self.debugPrint == true
{
print(“searchFilter--Response--SearchListViewController-----(String(describing: responseDict))-------------“)
}
if error == nil
{
let json = responseDict as! NSDictionary
if json[“statusCode”] != nil
{
let status = json[“statusCode”] as! Int
if status == 200
{
//self.updateSearchResults(for: UISearchController)
let result = json[“result”] as! NSArray
print(“Search result(result.count)“)
self.searchCollectionList1.addObjects(from: result as! [Any])
print(“searchCollectionList1(self.searchCollectionList1)“)
}
else
{
print(“json error status code not 200 getaccountInfo SearchListViewController”)
}
}
else
{
print(“json error getaccountInfo SearchListViewController”)
}
}
else
{
print(error?.localizedDescription ?? “getaccountInfo got with error”)
}
}
}
- 当我单击例如A的字母时,其数据来自服务器 但不填写集合视图。
- 当我点击字母S时,一个值 资产正在填充集合 view.AS 值显示 从 AS 值中删除 A。
你应该把你的重新加载相关的代码放在异步块中。
if status == 200
{
let result = json[“result”] as! NSArray
print(“Search result(result.count)“)
self.searchCollectionList1.addObjects(from: result as! [Any])
print(“searchCollectionList1(self.searchCollectionList1)“)
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
现在,您可以在以下函数中注释此行
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
// YOUR EXISTING CODE HERE
// Comment the line to reload the collectionView, as we have already doing this in block.
//self.collectionView.reloadData()
}
尝试分享您获得的结果!