我是Swift的初学者。
我目前正在实施一个可以搜索多个字段的搜索栏。
据我所知,我知道我们需要一个用于iOS swift搜索栏的数组。
以下是我目前的代码。我不知道为什么我的搜索没有结果,我已经确认我的数组中有数据。
updateSearchResultsForSearchController。
func updateSearchResultsForSearchController(searchController: UISearchController) {
filtered.removeAll(keepCapacity: false)
for i in 0...arrProcessName.count{
if(arrProcessName[safe: i] != nil && arrRequestedBy[safe: i] != nil && arrCreationTime[safe: i] != nil && arrStatus[safe: i] != nil)
{
allItems += [(process:arrProcessName[i], requestor:arrRequestedBy[i], creation:arrCreationTime[i], status:arrStatus[i])]
}
}
tableView?.reloadData()
}
搜索栏。
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
print("testing", searchText)
filtered = allItems.filter({ data in
let tmp: NSString = data.process
let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
return range.location != NSNotFound
})
if(filtered.count == 0){
searchActive = false;
} else {
searchActive = true;
}
tableView?.reloadData()
}
的tableView。
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.searchCtrler.active {
return filtered.count
} else {
return arrRequestedBy.count
}
}
tableView with indexPath。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
removeProgressBar()
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! HistoryCustomCell
cell.selectionStyle = .None
cell.accessoryType = UITableViewCellAccessoryType.None
if self.searchCtrler.active {
cell.lbl_request_name.text = filtered[indexPath.row].process
cell.lbl_requested_by.text = filtered[indexPath.row].requestor
cell.lbl_creation_time.text = filtered[indexPath.row].creation
cell.lbl_status.text = filtered[indexPath.row].status
} else {
cell.lbl_request_name.text = arrProcessName[indexPath.row]
cell.lbl_requested_by.text = "Requested by: " + arrRequestedBy[indexPath.row]
cell.lbl_creation_time.text = arrCreationTime[indexPath.row]
cell.lbl_status.text = arrStatus[indexPath.row]
switch arrStatus[indexPath.row] {
case "Completed", "Approved":
cell.img_status.image = UIImage(named: "ic_approved")
case "Running", "Processing", "Pending":
cell.img_status.image = UIImage(named: "ic_pending")
case "Denied", "Rejected":
cell.img_status.image = UIImage(named: "ic_reject")
case "Error":
cell.img_status.image = UIImage(named: "ic_terminated")
case "Retracted":
cell.img_status.image = UIImage(named: "ic_retracted")
default:
cell.img_status.image = UIImage(named: "")
}
}
return cell
}
我现在的情况是,当我使用搜索栏时,我得不到任何结果。请帮忙。我真的无法找到有关在互联网上拥有多个搜索字段的大量信息。您的回答将不胜感激。