使用Swift 3,该应用程序是一个博客阅读器,使用PHP和JSON从MYSQL数据库读取。
目前我的SearchBar没有按照我想要的方式进行操作,我让它在我的mainArray(第1部分)中使用' All'范围。在进行过滤时,正在过滤的对象将移至filteredArray。我这样做是因为我无法弄清楚如何让它做我想做的事。
它应该做的是,当用户正在搜索一个对象时,我想让对象显示在mainArray或者followArray中,而不是将它移动到另一个数组,所以它没有结合。基本上过滤tableview并不删除任何部分或组合任何对象,因为它会使用户不知道该对象所在的部分,并且当然要确保范围栏正常工作。
学习如何实施搜索栏,以便在我尝试进一步提升水平时看到我的麻烦。谢谢!
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()
}
}
答案 0 :(得分:2)
现在您创建一个数组(filteredArray
),并假设您在搜索时有1个部分。
我会删除这个假设。
在filterContentForSearchText
方法中,创建一个数组数组(其中每个内部数组代表一个部分)。
然后更新所有表视图数据源方法以使用该数组数组来获取正确的值。
首先,将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()
}