Swift:使用SearchBarsearch这两个部分,而不是合并它们

使用Swift 3,该应用程序是使用PHP和JSON从MYSQL数据库读取的博客阅读器。

目前我的SearchBar没有做我想做的事情,我在mainArray(第一部分)中search'All'范围。 当它被过滤时,被过滤的对象被移动到filteredArray。 我同时做了这个,因为我无法弄清楚如何让它做我想做的事情。

它应该做的是这样的,当用户search一个对象时,我希望对象显示在mainArray或者followedArray中,而不是将它移动到不同的数组,所以它不会合并。 基本上过滤tableview,而不是删除任何部分或组合任何对象,因为它会混淆用户通过不知道该对象在哪个部分,当然,确保范围栏正常工作。

学习如何实施一个search栏,以便当我尝试进一步提高水平时,可以看到我的麻烦。 谢谢!

SearchBar和范围代码

let searchController = UISearchController(searchResultsController: nil) override func viewDidLoad() { // Search Bar searchController.searchResultsUpdater = self searchController.dimsBackgroundDuringPresentation = false definesPresentationContext = true myTableView.tableHeaderView = searchController.searchBar searchController.searchBar.backgroundColor = UIColor.white searchController.searchBar.barTintColor = UIColor.white // Scope Bar searchController.searchBar.scopeButtonTitles = ["All", "Released", "Unreleased", "Free"] searchController.searchBar.delegate = self } // Title for Header func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { if !(searchController.isActive && searchController.searchBar.text != "") { if section == 0 { return "Followed Blogs" } else { return "All Blogs" } } return "Filtered Blogs" } // Number of Rows in Section func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if !(searchController.isActive && searchController.searchBar.text != "") { if section == 0 { return followedArray.count } else if (section == 1) { return mainArray.count } } return filteredArray.count } // Number of Sections func numberOfSections(in tableView: UITableView) -> Int { if !(searchController.isActive && searchController.searchBar.text != "") { return 2 } return 1 } // CellForRowAt indexPath public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let CellIdentifier = "Cell" var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell if cell != cell { cell = CustomCell(style: UITableViewCellStyle.default, reuseIdentifier: CellIdentifier) } // Configuring the cell var blogObject: Blog if !(searchController.isActive && searchController.searchBar.text != "") { if indexPath.section == 0 { blogObject = followedArray[indexPath.row] cell.populateCell(blogObject, isFollowed: true, indexPath: indexPath, parentView: self) } else if indexPath.section == 1 { blogObject = mainArray[indexPath.row] cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self) } } else { blogObject = filteredArray[indexPath.row] cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self) } return cell } // SEARCH BAR: Filtering Content func filterContentForSearchText(searchText: String, scope: String = "All") { filteredArray = mainArray.filter { Blog in let categoryMatch = (scope == "All") || (Blog.blogType == scope) return categoryMatch && (Blog.blogName.lowercased().contains(searchText.lowercased())) } myTableView.reloadData() } // SEARCH BAR: Updating Results func updateSearchResults(for searchController: UISearchController) { filterContentForSearchText(searchText: searchController.searchBar.text!) } // SEARCH BAR: Scope func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) { filterContentForSearchText(searchText: searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope]) } // SEARCH BAR: Updating Scope func updateSearchResultsForSearchController(searchController: UISearchController) { let searchBar = searchController.searchBar let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex] filterContentForSearchText(searchText: searchController.searchBar.text!, scope: scope) } // Deallocating Search Bar deinit{ if let superView = searchController.view.superview { superView.removeFromSuperview() } } 

现在你创build一个单一的数组( filteredArray ),并假设你search时有1节。

我会删除这个假设。

filterContentForSearchText方法内部,创build一个数组数组(其中每个内部数组表示一个节)。

然后更新所有的表视图数据源方法,以使用该数组数组来获得正确的值。

首先,将你的filteredArray声明更新为一个数组数组。

现在更新你的表格视图方法:

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if !(searchController.isActive && searchController.searchBar.text != "") { if section == 0 { return followedArray.count } else { return mainArray.count } } else { return filteredArray[section].count } } // Number of Sections func numberOfSections(in tableView: UITableView) -> Int { if !(searchController.isActive && searchController.searchBar.text != "") { return 2 } else { return filteredArray.count } } // CellForRowAt indexPath public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let CellIdentifier = "Cell" var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell if cell != cell { cell = CustomCell(style: UITableViewCellStyle.default, reuseIdentifier: CellIdentifier) } // Configuring the cell var blogObject: Blog if !(searchController.isActive && searchController.searchBar.text != "") { if indexPath.section == 0 { blogObject = followedArray[indexPath.row] cell.populateCell(blogObject, isFollowed: true, indexPath: indexPath, parentView: self) } else { blogObject = mainArray[indexPath.row] cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self) } } else { blogObject = filteredArray[indexPath.section][indexPath.row] cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self) } return cell } 

最后更新filterContentForSearchText方法:

 func filterContentForSearchText(searchText: String, scope: String = "All") { let filteredFollowed = followedArray.filter { Blog in let categoryMatch = (scope == "All") || (Blog.blogType == scope) return categoryMatch && (Blog.blogName.lowercased().contains(searchText.lowercased())) } let filteredMain = mainArray.filter { Blog in let categoryMatch = (scope == "All") || (Blog.blogType == scope) return categoryMatch && (Blog.blogName.lowercased().contains(searchText.lowercased())) } filteredArray = [ filteredFollowed, filteredMain ] myTableView.reloadData() }