ios - SearchBar 加载 plist 时出现问题

标签 ios swift xcode uitableview uisearchbar

我正在将我的 plist 加载到 TableView 中,一切正常,但现在我试图在 Page1 上包含一个 SearchBar。在下面你可以看到 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] = []
}

到这里,一切都运行良好!现在我在尝试插入 SearchView 时遇到了麻烦,看看我到目前为止做了什么:

class Page1: UITableViewController, UISearchBarDelegate {

    @IBOutlet weak var searchBar: UISearchBar!

    let employeesSearching: [String] = [String]()  //now using: 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..<self.Shared.instance.itens.count {
                let listItem : String = self.Shared.instance.itens[i]
                if listItem.lowercased().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


编辑 1 经过提示,现在唯一的麻烦是: enter image description here


编辑 2 现在我有这个: enter image description here

最佳答案

错误是因为 employeesSearchingString 的常量数组。

您可能需要一个 Employee 的可变数组。

改变:

let employeesSearching: [String] = [String]()

到:

var employeesSearching = [Employee]()

关于ios - SearchBar 加载 plist 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43217922/

相关文章:

ios - 更新后 Xcode 中 app.dsym 中的警告

ios - 如何解码 BLE 广告数据

javascript - 解析服务器向用户数组推送通知

ios - 如何通过 ViewControllers 管理和释放内存

swift - 我的字符串扩展在 appDelegate 之前运行,为什么会这样?

ios - 如何在 Swift 中使用 PromiseKit 和 Firebase?

ios - 应用程序包不包含有效的 IOS 标识符

c++ - 将不同组中的 C++ 文件包含到 Objective-C++ 测试中

ios - 如何在推送通知 iOS Swift 中终止应用程序时调用 API?

ios - 核心数据 - 在 objective-c 项目 Xcode 8 中子类化 NSManagedObject 的正确方法