gpt4 book ai didi

ios - Swift:让 SearchBar 搜索这两个部分而不是将它们合并

转载 作者:可可西里 更新时间:2023-11-01 01:18:44 25 4
gpt4 key购买 nike

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

目前我的 SearchBar 没有按照我的意愿进行,我让它在我的 mainArray(第 1 部分)中使用“全部”范围进行搜索。当它被过滤时,被过滤的对象被移动到 filteredArray。我同时这样做是因为我不知道如何让它做我想做的事。

它应该做的是,当用户正在搜索一个对象时,我希望该对象显示在 mainArray 或 followedArray 中,而不是将它移动到不同的数组,这样它就不会合并。基本上过滤 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()
}
}

最佳答案

现在您创建一个数组 (filteredArray) 并假设您在搜索时有 1 个部分。

我会删除该假设。

在您的 filterContentForSearchText 方法中,创建一个数组数组(其中每个内部数组代表一个部分)。

然后更新所有 TableView 数据源方法以使用该数组数组来获取正确的值。

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

现在更新你的 TableView 方法:

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()
}

关于ios - Swift:让 SearchBar 搜索这两个部分而不是将它们合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44959860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com