Я сделал свой код для поиска, и он показывает ошибку. Кто-нибудь может мне помочь? Спасибо
import UIKit
class streetwearbrand{
var brandkeyword : String?
var brandname : [String]?
init(brandkeyword: String, brandname:[String]) {
self.brandkeyword = brandkeyword
self.brandname = brandname
}
}
class ViewController: UIViewController {
@IBOutlet weak var SignUpButton: UIButton!
@IBOutlet weak var LoginButton: UIButton!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
var Streetwearbrand = [streetwearbrand]()
var searchBrand = [String]()
var searching = false
override func viewDidLoad() {
super.viewDidLoad()
Streetwearbrand.append(streetwearbrand.init(brandkeyword: "A", brandname:["A Cold Wall", "Acme De La Vie", "Adidas", "Anti Social Social Club"]))
Streetwearbrand.append(streetwearbrand.init(brandkeyword: "B", brandname:["Balenciaga", "Bape", "Billionaries Boys Club"]))
Streetwearbrand.append(streetwearbrand.init(brandkeyword: "C", brandname:["Carhartt", "Cav Empt", "CDG Play", "Champion"]))
Streetwearbrand.append(streetwearbrand.init(brandkeyword: "D", brandname:["10 Deep"]))
Streetwearbrand.append(streetwearbrand.init(brandkeyword: "E", brandname:["Emotionally Unavailable", "Essential"]))
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate{
func numberOfSections(in tableView: UITableView) -> Int {
return Streetwearbrand.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searching {
return searchBrand.count
} else {
return Streetwearbrand[section].brandname?.count ?? 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if searching{
cell.textLabel?.text = searchBrand[indexPath.row]
} else {cell.textLabel?.text = Streetwearbrand[indexPath.section].brandname?[indexPath.row]
}
return cell
}
//for title
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return Streetwearbrand[section].brandkeyword
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 40))
view.backgroundColor = .lightGray
let label = UILabel(frame: CGRect(x: 15, y: 0, width: view.frame.width - 15, height: 40))
label.text = Streetwearbrand[section].brandkeyword
view.addSubview(label)
return view
}
//to generate the height of the keyword label
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
// search
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchBrand = Streetwearbrand.filter({$0.prefix(searchText.count) == searchText.lowercased()})
searching = true
tableView.reloadData()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searching = false
searchBar.text = ""
tableView.reloadData()
}
}