2017-04-05 74 views
0

我正在將我的plist加載到TableView中,它會一切正常,但是現在,當我搜索一些不考慮第一個字母的東西時。下面你看到的directory.plist和我Main.storyboard不搜索第一個字母

plist and storyboard

要正確加載的plist我把下面的代碼放在我的didFinishLaunchingWithOptions

class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     if let url = Bundle.main.url(forResource: "directory", withExtension: "plist"), let array = NSArray(contentsOf: url) as? [[String:Any]] { 
      Shared.instance.employees = array.map{Employee(dictionary: $0)} 
     } 
     return true 
} 

我也有一個結構幫助我加載所有我的東西:

struct EmployeeDetails { 
    let functionary: String 
    let imageFace: String 
    let phone: String 

    init(dictionary: [String: Any]) { 
     self.functionary = (dictionary["Functionary"] as? String) ?? "" 
     self.imageFace = (dictionary["ImageFace"] as? String) ?? "" 
     self.phone = (dictionary["Phone"] as? String) ?? "" 
    } 
} 
struct Employee { 
    let position: String 
    let name: String 
    let details: [EmployeeDetails] // [String:Any] 

    init(dictionary: [String: Any]) { 
     self.position = (dictionary["Position"] as? String) ?? "" 
     self.name = (dictionary["Name"] as? String) ?? "" 

     let t = (dictionary["Details"] as? [Any]) ?? [] 
     self.details = t.map({EmployeeDetails(dictionary: $0 as! [String : Any])}) 
    } 
} 

struct Shared { 
    static var instance = Shared() 
    var employees: [Employee] = [] 
} 

直到這裏,一切都運行良好!現在我成了有問題,當我試圖插入一個搜索查看,看看我做了什麼至今:

class Page1: UITableViewController, UISearchBarDelegate { 

    @IBOutlet weak var searchBar: UISearchBar! 

    var employeesSearching = [Employee]() 
    var isSearching : Bool = false 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.searchBar.delegate = self 
    } 

    override func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if self.isSearching == true { 
      return self.employeesSearching.count 
     } else { 
      return Shared.instance.employees.count 
     } 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell1 
     let employee = Shared.instance.employees[indexPath.row] 

     if self.isSearching == true { 
      cell.nameLabel.text = self.employeesSearching[indexPath.row].name 
      cell.positionLabel.text = self.employeesSearching[indexPath.row].position 
     } else { 
      cell.nameLabel.text = employee.name 
      cell.positionLabel.text = employee.position 
     } 
     return cell 
    } 

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 
     if self.searchBar.text!.isEmpty { 
      self.isSearching = false 
      self.tableView.reloadData() 
     } else { 
      self.isSearching = true 
      self.employeesSearching.removeAll(keepingCapacity: false) 
      for i in 0..<Shared.instance.employees.count { 
       let listItem : Employee = Shared.instance.employees[i] 
       if listItem.name.range(of: self.searchBar.text!.lowercased()) != nil { 
        self.employeesSearching.append(listItem) 
       } 
      } 
      self.tableView.reloadData() 
     } 
    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if let destination = segue.destination as? Page2, 
      let indexPath = tableView.indexPathForSelectedRow { 
      destination.newPage = Shared.instance.employees[indexPath.row] 
     } 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 
} 

我有對我的搜索的第一個字母的麻煩。請看:

enter image description here enter image description here

回答

2

的問題是這一行:

if listItem.name.range(of: self.searchBar.text!.lowercased()) != nil { 

您正在尋找僱員姓名的常規文本中搜索文本的小寫版本。

「John Smith」文本不包含搜索文本「j」。但它包含搜索文本「ohn」。

速戰速決是到該行的代碼更改爲:

if listItem.name.lowercased().range(of: self.searchBar.text!.lowercased()) != nil { 

現在,這兩個比較的員工姓名,搜索文本的小寫版本。所以現在它會匹配,因爲「約翰史密斯」包含「j」。

順便說一句 - 它一遍又一遍地小寫搜索文本效率低下。還有更好的方法來編寫循環代碼。我將其更改爲:

self.employeesSearching.removeAll(keepingCapacity: false) 
let searchText = self.searchBar.text!.lowercased() 
for employee in Shared.instance.employees { 
    if employee.name.lowercased().range(of: searchText) != nil { 
     self.employeesSearching.append(employee) 
    } 
} 

而且更簡單的方法是替換代碼:

let searchText = self.searchBar.text!.lowercased() 
self.employeesSearching = Shared.instance.employees.filter { $0.name.lowercased().range(of: searchText) != nil 
} 

要搜索的文本名稱或位置,只需更新比較表達式:

if employee.name.lowercased().range(of: searchText) != nil || employee.position.lowercased().range(of: searchText) != nil { 

如果您使用filter做出類似更改。

+0

太棒了!它完成了,現在只是爲了知識。我應該改變什麼,不僅要搜索「名稱」,還要搜索「位置」? –

+1

查看我的更新回答。這是一些非常基本的東西。我懇請您花時間閱讀Apple的「Swift編程語言」一書。你越懂語言,你就越好。 – rmaddy

+0

我即將這樣做,比你這麼多的課程! –

相關問題