【问题标题】:Trouble on SearchBar loading plistSearchBar 加载 plist 出现问题
【发布时间】:2017-08-30 06:50:10
【问题描述】:

我正在将我的 plist 加载到 TableView 中,一切正常,但现在我试图在 Page1 上包含一个 SearchBar。下面你会看到 directory.plist 和我的 Main.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()
    }
}

这些正是错误:


编辑 1 提示后,现在唯一的麻烦是:


编辑 2 现在我有这个:

【问题讨论】:

  • 我回滚了您的最后一次更改,因为它是关于一个完全不同的问题。如果需要,发布一个新问题。

标签: ios swift xcode uitableview uisearchbar


【解决方案1】:

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

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

变化:

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

到:

var employeesSearching = [Employee]()

【讨论】:

  • 很好!!现在我遇到了最后一个麻烦,我刚刚更新了问题来向您展示。最后一行错了。
  • 你不应该那样重做你的问题。这让我的回答显得不恰当。如果有的话,添加新图像以显示附加错误。但你可能想要Shared.instance.employees.count 而不是self.Shared.instance.items.count
  • 您需要像修复第 57 行一样修复第 58 行。员工列表不是String,而是Employee
  • 查看String 的文档。您可能想在listItem.name 上致电lowercased()
  • 如果需要,这将是一个全新问题的主题。这个问题已经回答并解决了。如果您认为它解决了您最初的问题,您应该通过接受我的回答来结束此问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-08
  • 2011-11-07
  • 2015-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多