不搜索第一个字母

问题描述:

我正在将我的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

的问题是这一行:

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

我即将这样做,比你这么多的课程! –