问题
I am a beginner in Swift.
I am currently implementing a search bar that can searched by multiple fields.
From my knowledge, I know that we need an array for iOS swift search bar.
Below are the codes that I am having currently. I have no idea why my search has no result and I have already confirmed that I have data in my array.
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()
}
searchBar.
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
}
My situation now is that I don't get any result when I am using my search bar. Please help. I really can't find much information about having multiple search field on the Internet. Your answers will be appreciated.
来源:https://stackoverflow.com/questions/40606738/ios-swift-search-bar