2016-12-14 5 views
1
私はフィルタリングし、その1つの結果だけを示して検索バーを持っていると思い Class viewController

フィルタ結果(S)クラスの配列で(スウィフト3)

var allObjects = [Objects]() 
    var inSearchMode = false 

    @IBOutlet weak var searchBar: UISearchBar! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     searchBar.delegate = self 

    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 


     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Cell 


     if inSearchMode { 

      let fill: Objects! 
      fill = filteredObject[indexPath.row] 
      cell.configureCell(fill) 

     } else { 

      let fill: Objects! 
      fill = allObjects[indexPath.row] 
      cell.configureCell(fill) 

      return cell 
     } 

    } 

    return UITableViewCell() 

    } 

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

     if inSearchMode { 

      return filteredObject.count 

     } 

     return allObjects.count 

     } 

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

     return 1 

    } 

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

     if searchBar.text == nil || searchBar.text == "" { 

      inSearchMode = false 
      tableView.reloadData() 
      view.endEditing(true) 

     } else { 

      inSearchMode = true 
      var lowerCase = Int(searchBar.text!) 
      filteredObject = allObjects.filter({$0.number == lowerCase}) 
      tableView.reloadData() 

      print(filteredObject) 

     } 

    } 

class Objects { 

    var number: Int! 
    var name: String! 

    init(number: Int, name: String) { 
     self.number = number 
     self.name = name 
    } 
}   

私たちが探している番号が含まれています。私はcontainsを使用し、検索バーから入力した数字を入力することを考えました。

私はfilteredObjectに一つのオブジェクトを取得するために管理し、それは私がテーブルビューを駆動するために計算されたプロパティを使用しますtableView

答えて

3

はあなたのコードのこの部分で十分に確認してください:

if inSearchMode { 

    let fill: Objects! 
    fill = filteredObject[indexPath.row] 
    cell.configureCell(fill) 

} else { 

    let fill: Objects! 
    fill = allObjects[indexPath.row] 
    cell.configureCell(fill) 

    return cell 
} 

あなたが検索モードであるときは、セルを返しませんでしたつまり、検索するときに何も表示されないのはそのためです。

+0

これは完全に機能します。プロジェクトが大きくなったら、この小さな細部を見逃すことはかなり普通です。ありがとうございます。 – Christian

+0

この状況を防ぐための安全な習慣として、私は通常、最初の行のセルをデキューして最後に戻します。間違っていればセルが返ってきて、多分ちょうど間違った内容でちょうどいいです。何らかの理由で空のセルを返す必要がある場合は、何も返さないでreturn nilを呼び出すと、何も返されていないことが明確になります –

2

に表示されません。このプロパティは、すべてのオブジェクトまたはフィルタリングオブジェクト(複数可)のいずれかである:

var allObjects = [Objects]() 
var filteredObjects: [Objects]? 

var objects: [Objects] = { 
    return filteredObjects ?? allObjects 
} 

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


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

    let fill = self.objects[indexPath.row] 
    cell.configureCell(fill) 

    return cell 
} 

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

    if searchBar.text == nil || searchBar.text == "" { 
     self.filteredObjects = nil 
    } else { 
     var lowerCase = Int(searchBar.text!) 
     self.filteredObjects = allObjects.filter({$0.number == lowerCase}) 
    } 
    tableView.reloadData() 
} 
関連する問題