I am doing a filtering operation using UITextfield. As it should be, if textfield.isEmpty = true, I run the getCharacters() function that I created in the viewModel, while in the other case I run the function that I wrote the filtering logic for. All operations are running as they should, but there is a problem. In this problem, after the textField performs the first search and deletes it, it needs to run the getCharacters() function again, but it continues to show the results of the last search on the TableView screen. I will also share the source codes of the relevant parts and the screenshot of the problem in the attachment.
[![I can do filtering properly][2]][2]
[![When the textfield is empty, it does not reload all characters in my table view. It stucks at the previous search result.][3]][3]
As you can see, I can do filtering as i want. [2]: https://i.stack.imgur.com/PNqwr.png But when i delete the texts in the textfield, it does not reload all cells in my table view. [3]: https://i.stack.imgur.com/VFshl.png
extension CharactersViewController: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// Yeni metni oluştur
let textToEnter = (textField.text as NSString?)?.replacingCharacters(in: range, with: string) ?? ""
if textToEnter.count < 3 {
// Textfield tamamen boş ise
DispatchQueue.main.async {
self.viewModel.getCharacters()
self.tableView.reloadData()
}
} else if textToEnter.count >= 3 {
// Metin uzunluğu 3 veya daha fazlaysa
self.viewModel.searchCharacters(with: textToEnter)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
print(textToEnter)
return true
}
}