gpt4 book ai didi

iOS - UISearchBar 动态搜索结果在快速输入时不起作用(快速)

转载 作者:行者123 更新时间:2023-11-30 12:33:35 24 4
gpt4 key购买 nike

我想要的是一个搜索栏,当用户在搜索栏中输入每个字符时,它会使用用户名更新表格 View 。当用户删除(退格)搜索栏中的所有字符时,即搜索栏为空,我希望表格 View 不显示用户名并且为空。

我有一个半功能的 UISearchbar,可以搜索用户名(解析后端)。当我在搜索栏中缓慢地逐个字符输入时,它工作得很好。

但是,当我快速输入时,搜索结果有点偏离 - 它显示了之前输入的字符中的用户名。例如:如果我快速输入“nik”,它仍然会显示带有“ni”的用户名。另外,当我快速退格直到搜索栏中没有字符时,它仍然会显示某些用户名。

同样,当我缓慢输入字符时,它工作得很好,但我觉得它不是很有效。我尝试了各种各样的方法,但到目前为止没有任何效果。我已在下面添加了相关代码,如果您需要更多信息,请告诉我。

    // called when keyboard search button pressed
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

let searchString = searchBar.text;

if (searchString != "") {
//do load users
loadUsers(searchText: searchString!);
print("called from searchBarSearchButtonClicked");
}

}

// called when cancel button pressed
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {

searchBar.text = "";

//hide tableview maybe?!
//searchTableView.isHidden = true;

}

func updateSearchResults(for searchController: UISearchController) {
let searchString: String = (searchController.searchBar.text)!;


/*if (searchString == "" && !searchActive) {
searchResults.removeAll(keepingCapacity: false);
searchTableView.reloadData();
print("test");
}*/

if (searchString != "" && !self.searchActive) {
loadUsers(searchText: searchString);
print("called from updateResults");
}

if (searchString == "") {
searchResults.removeAll(keepingCapacity: false);
searchTableView.reloadData();
print("test");
}
}


func loadUsers(searchText: String ) {

if(searchText != "") {


let usernameQuery = PFUser.query();
//usernameQuery?.whereKey("username", equalTo: searchText);
usernameQuery?.whereKey("username", contains: searchText.lowercased());


let fbUsername = PFUser.query();
fbUsername?.whereKey("username", contains: searchText);


let firstnameQuery = PFUser.query();
//firstnameQuery?.whereKey("last_name", equalTo: searchText)
firstnameQuery?.whereKey("last_name", contains: searchText);


let lastnameQuery = PFUser.query();
//lastnameQuery?.whereKey("first_name", equalTo: searchText);
lastnameQuery?.whereKey("first_name", contains: searchText);


let query = PFQuery.orQuery(withSubqueries: [usernameQuery!,fbUsername!, firstnameQuery!, lastnameQuery!]);

searchActive = true;

query.findObjectsInBackground { (objects, error) in

if error != nil {

print("There was an error getting userlist");

}

else {

if let object = objects {

self.searchResults.removeAll(keepingCapacity: false);

for object in objects! {

let user = object.object(forKey: "username") as! String;
self.searchResults.append(user);

}

//self.searchTableView.reloadData();


DispatchQueue.main.async {

self.searchTableView.reloadData();

//self.userSearchController.searchBar.resignFirstResponder();

}

}

}

self.searchActive = false;

}
}

}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if(userSearchController.isActive) {

print(searchResults.count);
return searchResults.count;

}

return 0;
}


public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath);

if(self.userSearchController.isActive && searchResults.count > indexPath.row) {

print(searchResults[indexPath.row]);
cell.textLabel?.text = searchResults[indexPath.row];

}

return cell;

}




/*
public func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
return true;
}

public func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
return true;
}



func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {

//loadUsers(searchText: (searchBar.text)!);
} */


func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

if (searchBar.text != "" ) {
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(loadUsers(searchText:)), object: searchBar.text! as String);


perform(#selector(loadUsers(searchText:)), with: searchBar.text! as String, afterDelay: 0.50);


print("called from textDidChange");

}
if (searchBar.text == "") {

searchResults.removeAll(keepingCapacity: false);
searchTableView.reloadData();
print("test2");
}

}

最佳答案

我通过使用计时器解决了这个问题。下面的代码在调用您的函数之前等待一秒钟。如果在那一秒内发生文本更改,它将使先前安排的计时器无效,并且只执行第二个计时器。您可以通过在函数中打印搜索文本来检查这一点。

var timer: Timer?

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
timer?.invalidate() //cancels out previous Timers
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(myFunctionThatDoesServerSearch), userInfo: nil, repeats: false)
}

关于iOS - UISearchBar 动态搜索结果在快速输入时不起作用(快速),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43143482/

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