我看过但找不到合适的答案。 我有一个填写了药物名称的tableview。药物已经输入,因为它们已从文本中遇到Xcode,如下所示。
class MasterViewController: UITableViewController {
// MARK: - Properties
var detailViewController: DetailViewController? = nil
var drugs = [Drug]()
var filtereddrugs = [Drug]()
let searchController = UISearchController(searchResultsController: nil)
// MARK: - View Setup
override func viewDidLoad() {
super.viewDidLoad()
// Setup the Search Controller
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
definesPresentationContext = true
searchController.dimsBackgroundDuringPresentation = false
// Setup the Scope Bar
searchController.searchBar.scopeButtonTitles = ["All", "Green", "Blue", "Amber", "Red", "Black"]
tableView.tableHeaderView = searchController.searchBar
drugs = [
Drug(category:"Green", name:"Magnesium Trisilicate"),
Drug(category:"Green", name:"Co-magaldrox (Mucogel)"),
Drug(category:"Green", name: "Gastrocote"),
Drug(category:"Green", name:"Infant Gaviscon"),
Drug(category:"Green", name:"Peptac"),
Drug(category:"Green", name:"Mebeverine"),
Drug(category:"Green", name:"Fybogel Mebeverine"),
Drug(category:"Black", name:"Peppermint oil")]
if let splitViewController = splitViewController {
let controllers = splitViewController.viewControllers
detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
}
}
override func viewWillAppear(_ animated: Bool) {
clearsSelectionOnViewWillAppear = splitViewController!.isCollapsed
super.viewWillAppear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table View
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.isActive && searchController.searchBar.text != "" {
return filtereddrugs.count
}
return drugs.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let drug: Drug
if searchController.isActive && searchController.searchBar.text != "" {
drug = filtereddrugs[(indexPath as NSIndexPath).row]
} else {
drug = drugs[(indexPath as NSIndexPath).row]
}
cell.textLabel!.text = drug.name
cell.detailTextLabel!.text = drug.category
return cell
}
func filterContentForSearchText(_ searchText: String, scope: String = "All") {
filtereddrugs = drugs.filter({( drug : Drug) -> Bool in
let categoryMatch = (scope == "All") || (drug.category == scope)
return categoryMatch && drug.name.lowercased().contains(searchText.lowercased())
})
tableView.reloadData()
}
// MARK: - Segues
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let drug: Drug
if searchController.isActive && searchController.searchBar.text != "" {
drug = filtereddrugs[(indexPath as NSIndexPath).row]
} else {
drug = drugs[(indexPath as NSIndexPath).row]
}
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
controller.detailDrug = drug
controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
}
extension MasterViewController: UISearchBarDelegate {
// MARK: - UISearchBar Delegate
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
filterContentForSearchText(searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
}
}
extension MasterViewController: UISearchResultsUpdating {
// MARK: - UISearchResultsUpdating Delegate
func updateSearchResults(for searchController: UISearchController) {
let searchBar = searchController.searchBar
let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
filterContentForSearchText(searchController.searchBar.text!, scope: scope)
}
}
这个药物清单并不是完整的清单,实际上包含了很多,手动排序太多了!在接下来的几周里,我将进入更多。
我没有时间或力量将其更改为按字母顺序显示在表格视图中,我想知道几行代码可以解决问题吗?
提前干杯,
标记
答案 0 :(得分:5)
尝试添加此项,只要设置了属性,它就会对drugs
进行排序。
var drugs = [Drug]() {
didSet {
drugs.sort { $0.name < $1.name }
}
}