gpt4 book ai didi

swift 3 : Programmatically functioning scope bar

转载 作者:可可西里 更新时间:2023-11-01 01:22:42 26 4
gpt4 key购买 nike

我已经以编程方式为我的 TableView 设置了一个搜索栏,并向搜索栏添加了一个范围栏。我试图弄清楚当用户点击范围按钮之一时如何显示正确的信息。例如,如果他们点击“甜食”范围按钮,它只会显示数据模型中与甜食相关的所有水果。到目前为止,这是我的代码:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchResultsUpdating {
@IBOutlet weak var myTable: UITableView!

var fruits = [Fruits]()
var filteredFruits = [Fruits]()
var selectIndex: IndexPath = IndexPath()
var fruitName: String!
var myArray = ["All", "Sweet", "Fresh", "Other"]
var sweets = [String]()

var searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

fruits = [
Fruits(category: "Fresh", name: "Apple"),
Fruits(category: "Fresh", name: "Strawberry"),
Fruits(category: "Sweet", name: "Mango"),
Fruits(category: "Sweet", name: "Cherry"),
Fruits(category: "Sweet", name: "Grapes"),
Fruits(category: "Other", name: "Orange"),
Fruits(category: "Sweet", name: "Banana"),
Fruits(category: "Fresh", name: "Tomatoe"),
]

myTable.delegate = self
myTable.dataSource = self

searchController.dimsBackgroundDuringPresentation = false
self.definesPresentationContext = true
searchController.searchResultsUpdater = self
searchController.searchBar.scopeButtonTitles = ["All", "Sweet", "Fresh", "Other"]
searchController.searchBar.delegate = self
myTable.tableHeaderView = searchController.searchBar
searchController.searchBar.barTintColor = UIColor.white



}

func numberOfSections(in tableView: UITableView) -> Int {
return 1

}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if searchController.isActive && searchController.searchBar.text != "" {
return filteredFruits.count
}

return fruits.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
fruitName = fruits[indexPath.row].name
let cell = myTable.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
let fruit: Fruits
if searchController.isActive && searchController.searchBar.text != ""{
fruit = filteredFruits[indexPath.row]
} else {
fruit = fruits[indexPath.row]
}

if fruits[indexPath.row].category == "Sweet"{
sweets = [fruits[indexPath.row].category]
print(sweets)

}

cell.textLabel?.text = fruit.name
cell.detailTextLabel?.text = fruit.category


return cell
}

//SearchBar
func updateSearchResults(for searchController: UISearchController) {
let searchBar = searchController.searchBar
let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
filterContentsforSearchText(searchText: searchController.searchBar.text!, scope: scope)

}

func filterContentsforSearchText(searchText: String, scope: String = "All"){
filteredFruits = fruits.filter { fruit in
let categoryMatch = (scope == "All") || (fruit.category == scope)
return categoryMatch && fruit.name.lowercased().contains(searchText.lowercased())
}
myTable.reloadData()
}

func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
filterContentsforSearchText(searchText: searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])

if selectedScope == 1{
print("Sweet")
}

}



}

最佳答案

首先,让我们更新 filterContentsforSearchText 方法。如果 searchText 为空,您应该立即返回 categoryMatch:

func filterContentsforSearchText(searchText: String, scope: String = "All"){
filteredFruits = fruits.filter { fruit in
let categoryMatch = (scope == "All") || (fruit.category == scope)
if searchText.isEmpty {
return categoryMatch
}
return categoryMatch && fruit.name.lowercased().contains(searchText.lowercased())
}

myTable.reloadData()
}

然后,删除所有 searchController.searchBar.text != "" 语句,您不再需要它们。

最终代码:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchResultsUpdating {

@IBOutlet weak var myTable: UITableView!

var fruits = [Fruits]()
var filteredFruits = [Fruits]()
var selectIndex: IndexPath = IndexPath()
var fruitName: String!
var myArray = ["All", "Sweet", "Fresh", "Other"]
var sweets = [String]()

var searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

fruits = [
Fruits(category: "Fresh", name: "Apple"),
Fruits(category: "Fresh", name: "Strawberry"),
Fruits(category: "Sweet", name: "Mango"),
Fruits(category: "Sweet", name: "Cherry"),
Fruits(category: "Sweet", name: "Grapes"),
Fruits(category: "Other", name: "Orange"),
Fruits(category: "Sweet", name: "Banana"),
Fruits(category: "Fresh", name: "Tomatoe"),
]

myTable.delegate = self
myTable.dataSource = self

searchController.dimsBackgroundDuringPresentation = false
self.definesPresentationContext = true
searchController.searchResultsUpdater = self
searchController.searchBar.scopeButtonTitles = ["All", "Sweet", "Fresh", "Other"]
searchController.searchBar.delegate = self
myTable.tableHeaderView = searchController.searchBar
searchController.searchBar.barTintColor = UIColor.white
}

func numberOfSections(in tableView: UITableView) -> Int {
return 1

}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if searchController.isActive {
return filteredFruits.count
}

return fruits.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
fruitName = fruits[indexPath.row].name
let cell = myTable.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let fruit: Fruits
if searchController.isActive {
fruit = filteredFruits[indexPath.row]
} else {
fruit = fruits[indexPath.row]
}

if fruits[indexPath.row].category == "Sweet"{
sweets = [fruits[indexPath.row].category]
print(sweets)

}

cell.textLabel?.text = fruit.name
cell.detailTextLabel?.text = fruit.category


return cell
}

//SearchBar
func updateSearchResults(for searchController: UISearchController) {
let searchBar = searchController.searchBar
let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
filterContentsforSearchText(searchText: searchController.searchBar.text!, scope: scope)

}

func filterContentsforSearchText(searchText: String, scope: String = "All"){
filteredFruits = fruits.filter { fruit in
let categoryMatch = (scope == "All") || (fruit.category == scope)
if searchText.isEmpty {
return categoryMatch
}
return categoryMatch && fruit.name.lowercased().contains(searchText.lowercased())
}

myTable.reloadData()
}

func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
filterContentsforSearchText(searchText: searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])

print("selectedScope: \(selectedScope)")
if selectedScope == 1{
print("Sweet")
}

}
}

它如您所愿地工作!

enter image description here

关于 swift 3 : Programmatically functioning scope bar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43220754/

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