2017-04-04 80 views
0

我正在将我的plist加载到TableView中,并且它一切正常,但现在我正在尝试在Page1中包含一个SearchBar。下面你看到的directory.plist和我Main.storyboard搜索栏加载plist的问题

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! 

    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

+0

我回滚你最后的变化,因为它是关于一个完全不同的问题。根据需要发布新问题。 – rmaddy

回答

1

的错误是因为employeesSearchingString恒定阵列。

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

变化:

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

到:

var employeesSearching = [Employee]() 
+0

不错!现在我有最后一个麻烦了,我只是更新了这个问题来向你展示。最后一行错误。 –

+1

你不应该重做你的问题。这让我的回答看起来不合适。如果有的话,添加新的图像来显示额外的错误。但是你可能想要'Shared.instance.employees.count'而不是'self.Shared.instance.items.count'。 – rmaddy

+0

我重复了我的问题,像以前一样...它有点奏效,但出现了其他麻烦。在第58行('Value'type''page1'没有成员'Shared'),当我删除self时,我看到:(''不能将类型'Employee'的值转换为指定类型'String')第60行('不能将'String'类型的值转换为期望的参数类型'Employee'') –