首页 > 解决方案 > 在结构上使用 UISearchbar

问题描述

我的项目中有两个UITableViewControllers。第一个只是一个简单的tableView,显示一个普通的array. 第二个tableView是由Struct
UISearcBar在第一个中实现的,ViewController没有任何麻烦。我遵循了本指南这是我在第一个中用于搜索栏的代码viewController

let listArray = ["Item1", "Item2", "Item3", "Item4", "Item5"]
var searchData: [String]!        

 override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        searchBar.delegate = self
        searchData = listArray
    }     

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as UITableViewCell
        cell.textLabel?.text = searchData[indexPath.row]
        return cell
    }    

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return filteredData.count
    }

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

        searchData = searchText.isEmpty ? listArray : listArray { (item: String) -> Bool in
            return item.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil
        }

        tableView.reloadData()
    }

在第二个中ViewController,我使用 aStruct填充tableView
当我尝试实现 searchBar 时出现错误:

 struct ObjectModel {

        var objectProducer: String?
        var objecType: String?
        var unit: String?
        var contentSize: Double?
        var amount: Double?
        var addedDate: String?

    }    

     var objectList = [ObjectModel]()
        //Search data
      var filteredData = [ObjectModel]()    


        override func viewDidLoad() {
            super.viewDidLoad()
       //SearchData
            searchBar.delegate = self
            filteredData = objectList  //   
          }        

     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
           return filteredData.count
        }    

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ListCell") as! ListCell

            //the artist object
                   let object: ObjectModel

                   object = objectList[indexPath.row]

if activeSearch = false {    

              cell.objectNameLabel.text = "\(object.objectProducer ?? "Missing data"), \(object.objecType ?? "Missing data")"    
} else {
            cell.objectNameLabel.text = filteredData[indexPath.row] //Error: Cannot assign value of type 'ObjectModel' to type 'String?'    
}

            return cell

        }   


          func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

              filteredData = searchText.isEmpty ? objectList : objectList.filter { (item: String) -> Bool in //Error: 'String' is not convertible to 'ObjectModel'
                  return item.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil
              }

              tableView.reloadData()
          }    

使用时应该如何实现搜索Struct

标签: swiftuitableviewstructuisearchbar

解决方案


你必须改变

cell.objectNameLabel.text = filteredData[indexPath.row] 

   cell.objectNameLabel.text = filteredData[indexPath.row].objectProducer

因为在ObjectModelstruct 中,您必须分配必须分配的属性text之一UILabel。你可以分配

    var objectProducer: String?
    var objecType: String?
    var unit: String?
    var contentSize: Double?
    var amount: Double?
    var addedDate: String?

属性String说明。例如,如果您想分配金额,您可以使用

cell.objectNameLabel.text = "\(filteredData[indexPath.row].amount!)"

或者有更好的方法。

您必须扩展您的ObjectModelwithCustomStringConvertible以便您的所有结构变量都可以描述为字符串,然后它可以像那样工作

cell.objectNameLabel.text = filteredData[indexPath.row] 

你可以像下面这样扩展它。

extension ObjectModel: CustomStringConvertible{

  public var description: String { 
    return "amount \(self.amount) producer \(self.producer) bla bla"
  }

}

// 编辑

用主线程更改它。

tableView.reloadData()

至 :

DispatchQueue.main.async { 


   self.tableView.reloadData()

}

推荐阅读