2017-10-15 7 views
-1

検索せずに、セルをタップしたときにアラートが正しい姓を示しています。しかし、どういうわけか、名前を検索してセルをタップすると、アラートに間違った姓が表示されます。すべてのケースで(私は何でも検索する)アラートは常にQuuenの姓を示しています。私が検索すると結果は常に私のテーブルビューの最初のインデックスになると思う。SearchBarとTableviewに関する混乱

私のコードは:

import UIKit 

struct Human { 
    var name = String() 
    var surname = String() 

} 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate { 

    @IBOutlet weak var tableView: UITableView! 

    @IBOutlet weak var searchBar: UISearchBar! 

    var members = [Human]() 

    var filteredData = [String]() 

    var inSearchMode = false 

    var names = ["Oliver","Harold","John","Thea","Felicity"] 
    var surnames = ["Queen","Finch","Reese","Queen","Smoak"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 



     for i in 0..<names.count { 
      members.append(Human(name: names[i],surname: surnames[i])) 
     } 



     tableView.delegate = self 

     tableView.dataSource = self 

     searchBar.delegate = self 

     searchBar.returnKeyType = UIReturnKeyType.done 
    } 

    // MARK: - UITableViewDataSource 

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

     return 1 
    } 

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

     if inSearchMode { 

      return filteredData.count 
     } 

     return members.count 
    } 

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

     if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? DataCell { 

      let text: String! 

      if inSearchMode { 

       text = filteredData[indexPath.row] 

      } else { 

       text = members[indexPath.row].name 
      } 

      cell.congigureCell(text: text) 

      return cell 

     } else { 

      return UITableViewCell() 
     } 
    } 

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

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

      inSearchMode = false 

      view.endEditing(true) 

      tableView.reloadData() 

     } else { 

      inSearchMode = true 

      filteredData = names.filter { $0.lowercased().contains(searchBar.text!.lowercased()) } 

      tableView.reloadData() 

     } 
    } 
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     showAlert(title: "Surname is...", msg: members[indexPath.row].surname) 
    } 

} 

extension UIViewController { 

    func showAlert(title: String, msg: String) { 
     let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert) 
     let action = UIAlertAction(title: "OK", style: .default, handler: nil) 
     alert.addAction(action) 
     self.present(alert, animated: true, completion: nil) 
    } 
} 

やスクリーンショットはここにある:

https://imgur.com/a/aRMNe

最後に、どのように私はこの問題を解決することができますか?以下のような

+0

あなたの投稿されたコードは、分析するのに使用されていません。 –

+0

あなたの問題に関連しているとは思わないが、あなたの姓の配列に「Queen」が2回表示されます。 – Koen

答えて

0

用途:cellForRowdidSelectRowAttextDidChangeで変更Human

var filteredData = [Human]() 

変更filteredDataタイプを。 変更された行にコメントを追加しました。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? DataCell { 
     let text: String! 
     if inSearchMode { 
      text = filteredData[indexPath.row].name // changed this line only 
     } else { 
      text = members[indexPath.row].name 
     } 
     cell.textLabel?.text = text 
     return cell 
    } else { 
     return UITableViewCell() 
    } 
} 

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 
    if searchBar.text == nil || searchBar.text == "" { 
     inSearchMode = false 
     view.endEditing(true) 
     tableView.reloadData() 
    } else { 
     inSearchMode = true 
     filteredData = members.filter { $0.name.lowercased().contains(searchBar.text!.lowercased()) } // changed this line 
     tableView.reloadData() 
    } 
} 
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    if inSearchMode { 
     showAlert(title: "Surname is...", msg: filteredData[indexPath.row].surname) // added this line 
    } else { 
     showAlert(title: "Surname is...", msg: members[indexPath.row].surname) 
    } 
} 
+0

正常に動作しています!どうもありがとう ! – Oliver

関連する問題