Ios 不搜索第一个字母 P>我把我的PLIST装入一个TabLVIEW,一切都会好的,但是现在当我搜索某件东西时,它不会考虑第一个字母。下面是directory.plist和my Main.storyboard
要正确加载plist,我将以下代码放在我的Ios 不搜索第一个字母 P>我把我的PLIST装入一个TabLVIEW,一切都会好的,但是现在当我搜索某件东西时,它不会考虑第一个字母。下面是directory.plist和my Main.storyboard,ios,swift,uitableview,plist,uisearchbar,Ios,Swift,Uitableview,Plist,Uisearchbar,要正确加载plist,我将以下代码放在我的didfishlaunchingwithoptions: class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunch
didfishlaunchingwithoptions
:
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] = []
}
在这里之前,一切都很顺利现在,我在尝试插入SearchView时遇到了问题,请看一下我到目前为止所做的工作:
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()
}
}
类页面1:UITableViewController,UISearchBarDelegate{
@ibvar搜索栏:UISearchBar!
var employeesserching=[Employee]()
变量isSearching:Bool=false
重写func viewDidLoad(){
super.viewDidLoad()
self.searchBar.delegate=self
}
重写func numberOfSections(在tableView:UITableView中)->Int{
返回1
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
如果self.isSearching==true{
返回self.employeesserching.count
}否则{
返回Shared.instance.employees.count
}
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
将cell=tableView.dequeueReusableCell(标识符为“cell”,for:indexPath)设为!TableViewCell1
让employee=Shared.instance.employees[indexPath.row]
如果self.isSearching==true{
cell.namelab.text=self.employeesserching[indexPath.row].name
cell.positionLabel.text=self.employeesserching[indexPath.row].position
}否则{
cell.namelab.text=employee.name
cell.positionLabel.text=employee.position
}
返回单元
}
func searchBar(searchBar:UISearchBar,textDidChange searchText:String){
如果self.searchBar.text!.i为空{
self.issearch=false
self.tableView.reloadData()
}否则{
self.issearch=true
self.employeesserching.removeAll(保留容量:false)
对于0..中的i,问题是这一行:
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 {
这将比较员工姓名和搜索文本的小写版本。因此,现在它将匹配,因为“john smith”包含“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 {
如果使用过滤器
进行类似的更改,问题是这一行:
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 {
这将比较员工姓名和搜索文本的小写版本。因此,现在它将匹配,因为“john smith”包含“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 {
如果您使用过滤器,请进行类似的更改。太好了!现在就这样做,只是为了了解相关知识。我应该做些什么来不仅搜索名称
,而且搜索位置
?查看我的更新答案。这是一些非常基本的内容。我恳请您花时间阅读“Swift编程语言”苹果的书。你越懂这门语言,你的生活就会越好。我就要这么做了,比你还要多!我刚刚注意到,当我搜索“Suzan”然后触摸Suzan结果时,它显示了“John”的内容。你能看到我的代码有什么错误吗?太好了!现在就完成了,只是为了了解一下。我应该做些什么来不仅搜索名称
,而且搜索位置
?查看我的更新答案。这是一些非常基本的东西。我恳请你花时间阅读“Swift编程语言”苹果的书。你越懂这门语言,你的生活就会越好。我就要这么做了,比你还要多。我只是注意到,当我搜索“Suzan”,然后触摸Suzan结果时,它显示了“John”的内容。你能看到我的代码有什么错误吗?