2017-10-15 30 views
-1

的困惑沒有搜索,當我點擊任何單元格時,alert會顯示正確的姓氏。但是,不知何故,當我搜索一個名字並點擊它的單元格時,警報顯示錯誤的姓氏。在任何情況下(我的意思是,我搜索任何)警報總是顯示Quuen姓氏。我認爲,當我搜索時,結果總是成爲我的tableview的第一個索引。關於SearchBar和表格視圖

我的代碼:

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

我不認爲它與您的問題有關,但「女王」在您的姓氏數組中出現兩次。 – Koen

回答

0

使用如下:

更改filteredData類型Human

var filteredData = [Human]() 

cellForRowdidSelectRowAttextDidChange改變。 爲更改的行添加了註釋。

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