Что означает значение типа «streetwearbrand», не имеет префикса члена в коде панели поиска и как это исправить? - PullRequest
1 голос
/ 01 мая 2020

Я сделал свой код для поиска, и он показывает ошибку. Кто-нибудь может мне помочь? Спасибо

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

1 Ответ

0 голосов
/ 01 мая 2020
Streetwearbrand.filter({$0.prefix... 

В этом случае $0 - это элемент в массиве Streetwearbrand, который является экземпляром streetwearbrand.

Перво-наперво - типично называть класс заглавными буквами, а экземпляр - строчными, и, надеюсь, разными именами, чтобы не путать себя. Возможно, класс должен быть Streetwearbrand, а массив - streetweardbrandArray, но это личное предпочтение.

Ваша проблема в том, что класс streetwarebrand не реализует метод prefix. Я думаю, что вы хотите посмотреть на одно из свойств, которое является строкой, может быть brandkeyword как

Streetwearbrand.filter({$0.brandkeyword.prefix... 
...