ios - mvvm 中的 UITextfield 委托方法
问题描述
我需要在 mvvm 中搜索 tableview。
我的视图模型:- 搜索功能:-
func search(searchText :String?) {
filteredListArray = datasourceModel.dataListArray?.filter{($0.restaurantname?.range(of: searchText!, options: .caseInsensitive) != nil)}
}
在视图控制器中,我刚刚给出了文本字段委托方法。
我的视图控制器:-
class QM_SearchViewController: UIViewController,UITableViewDelegate,UITableViewDataSource, UISearchBarDelegate,UITextFieldDelegate {
@IBOutlet private weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var txt: UITextField!
var isSearching = false
var search:String=""
var filteredSearchArray = NSMutableArray()
private var searchViewModel :QM_SearchViewModel!
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?, withViewModel viewModel:QM_SearchViewModel) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
searchViewModel = viewModel
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
self.title = "SEARCH"
txt.delegate = self
searchViewModel.loadData { (isSuccess) in
if(isSuccess == true)
{
self.tableView.reloadData()
}
else{
}
}
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
print("While entering the characters this method gets called")
return true;
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool { //delegate method
return false
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool { //delegate method
textField.resignFirstResponder()
return true
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchViewModel.numberOfRowsInSection(section: section)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "searchcell"
var cell: QM_SearchCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? QM_SearchCell
if cell == nil {
tableView.register(UINib(nibName: "QM_SearchCell", bundle: nil), forCellReuseIdentifier: identifier)
cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? QM_SearchCell
}
cell.setsearchData(search: searchViewModel.datafordisplay(atindex: indexPath))
return cell
}
}
所以在文本字段委托中我可以如何搜索。如何从 viewmodel 中获取搜索功能。搜索 tableview 任务是如何发生的
解决方案
就在shouldChangeCharactersIn
你可以调用 searchViewModel
搜索方法,如果它没有触发自动更新你的数据源,你需要在调用搜索方法后触发你的数据源
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text,
let textRange = Range(range, in: text) {
let updatedText = text.replacingCharacters(in: textRange,
with: string)
if updatedText.count > 2 {
let result = searchViewModel.search(searchText :updatedText)
// result Should update your data Source searchViewModel.numberOfRowsInSection , and reload tableView
}
}
return true;
}
推荐阅读
- data-structures - 为什么在未排序数组中实现的优先级队列中的 Find-Minimum 操作只需要复杂度 = O(1) ?
- visual-studio-code - 有没有办法将 VS Code 片段文件放在非标准文件夹中并让 VS Code 使用它?
- configuration - 无法通过 carbon.xml 进行配置
- python - 为了创建字典,这些函数在做什么?
- c# - 如何进行并行数据库调用并绑定到模型 ASP.NET Core MVC
- unity3d - HoloLens 配套地图
- angular - 动态组件可以在表单中用作表单字段并且验证仍由父组件处理吗?
- swift - 以编程方式不改变宽度的 UISlider
- symfony4 - Symfony 4 handleRequest 运行查询
- javascript - 你可以在 javascript 的返回函数中使用父函数变量吗?