У меня есть табличное представление, которое получает данные json с моего сервера. Теперь я хочу добавить панель поиска, используя SearchContoller
. Ячейки (кастомные) правильно отображаются перед поиском:
но когда я начинаю печатать, ничего не отображается. Вот код:
МОДЕЛЬ
import Foundation
class PreviousLocations:NSObject{
let locationId: String?
let locationName: String?
let locationCity:String?
let locationCountry:String?
let locationLatitude:String?
let locationLongitude:String?
let sport:String?
init(dictionary:NSDictionary) {
locationId = dictionary["locationId"] as? String
locationName = dictionary["locationName"] as? String
locationCity = dictionary["locationCity"] as? String
locationCountry = dictionary["locationCountry"] as? String
sport = dictionary["sport"] as? String
locationLatitude = dictionary["locationLatitude"] as? String
locationLongitude = dictionary["locationLongitude"] as? String
}
}
Затем в PreviousLocationsViewControllors
var previousLocation = [PreviousLocations]()
var filteredPreviousLocations = [PreviousLocations]()
fun tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
}
if section == 1{
if (self.searchController.active) {
return filteredPreviousLocations.count
}else{
return previousLocation.count
}
}
return 1
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
if indexPath.section == 0{
let addLocationCell = self.tableView.dequeueReusableCellWithIdentifier("addLocationCell", forIndexPath: indexPath)
addLocationCell.textLabel?.text = "Add Location"
return addLocationCell
}else{
var locCells:PreviousLocationsTableCell
let locations : PreviousLocations
if (self.searchController.active) {
locCells = self.tableView.dequeueReusableCellWithIdentifier("previousLocationCell") as! PreviousLocationsTableCell
locations = filteredPreviousLocations[indexPath.row]
locCells.useLocations(locations)
}else{
locCells = self.tableView.dequeueReusableCellWithIdentifier("previousLocationCell", forIndexPath: indexPath) as! PreviousLocationsTableCell
locations = previousLocation[indexPath.row]
locCells.useLocations(locations)
}
return locCells
}
}
//MARK: - Search
func updateSearchResultsForSearchController(searchController: UISearchController)
{
filteredPreviousLocations.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF.locationName == %@", searchController.searchBar.text!)
let array = (previousLocation as NSArray).filteredArrayUsingPredicate(searchPredicate)
print(array)
filteredPreviousLocations = array as! Array
self.tableView.reloadData()
}
и пользовательская ячейка
import UIKit
import QuartzCore
class PreviousLocationsTableCell: UITableViewCell {
@IBOutlet weak var conteningView: UIView!
@IBOutlet weak var locatioNameLabel: UILabel!
@IBOutlet weak var locationCityLabel: UILabel!
@IBOutlet weak var sportImage: UIImageView!
func useLocations(location:PreviousLocations) {
conteningView.layer.masksToBounds = true
conteningView.exclusiveTouch = false
// Fill in the data
locatioNameLabel.text = location.locationName
locationCityLabel.text = location.locationCity
let imageSport = UIImage(named: "\(location.sport!).png")
sportImage.image = imageSport
func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
}
Если я попытаюсь изменить это
`filteredPreviousLocations = array as! Array`
в это
`filteredPreviousLocations = array as! [String]`
как описано в этом этом руководстве, получаю ошибку Cannot assign a value of type '[String]' to a value of type '[PreviousLocations]'