Swift 搜索控制器-使用NSPredicate with model时出现问题

Swift 搜索控制器-使用NSPredicate with model时出现问题,swift,nspredicate,uisearchcontroller,Swift,Nspredicate,Uisearchcontroller,我有一个表视图,它从我的服务器获取json数据。 现在我想使用searchcontroller添加一个搜索栏。搜索前正确显示单元格(自定义): 但当我开始打字时,什么也没有显示。代码如下: 模型 然后在以前的位置查看控制器 var previousLocation = [PreviousLocations]() var filteredPreviousLocations = [PreviousLocations]() fun tableView(tableView: UITableV

我有一个表视图,它从我的服务器获取json数据。 现在我想使用
searchcontroller
添加一个搜索栏。搜索前正确显示单元格(自定义):

但当我开始打字时,什么也没有显示。代码如下:

模型

然后在以前的位置查看控制器

 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]`
正如本教程中所解释的,我得到了错误
无法将“[String]”类型的值分配给“[PreviousLocations]”类型的值

属性的类型明确定义为
[PreviousLocations]
,因此除了在筛选行中,您不需要强制转换类型。格式字符串应为
locationName==%@

func updateSearchResultsForSearchController(searchController: UISearchController)
{
  let searchPredicate = NSPredicate(format: "locationName == %@", searchController.searchBar.text!)
  filteredPreviousLocations = (previousLocation as NSArray).filteredArrayUsingPredicate(searchPredicate)
  self.tableView.reloadData()
}
或者,您可以使用Swift的
过滤功能进行过滤

func updateSearchResultsForSearchController(searchController: UISearchController)
{
  filteredPreviousLocations = previousLocation.filter { $0.locationName == searchController.searchBar.text!}
  self.tableView.reloadData()
}
正如在这两个函数中一样,数组
filteredPreviousLocations
被明确设置,也不需要调用
removeAll

编辑:要在键入完整名称之前获得搜索结果,您应该筛选部分字符串,而不是整个位置名称

func updateSearchResultsForSearchController(searchController: UISearchController)
{
  filteredPreviousLocations = previousLocation.filter { $0.locationName.rangeOfString(searchController.searchBar.text!, options: [.AnchoredSearch, .CaseInsensitiveSearch, .DiacriticInsensitiveSearch]) != nil}
  self.tableView.reloadData()
}

@在第一个解决方案中,我得到的'Value of type'[PreviousLocations]'没有成员'FilteredarrayingPredicate',而在第二个解决方案中,当我开始键入tanks时,我仍然得到一个空表,但现在'Cannot'assign of type'[AnyObject]'给'PreviousLocations]类型的值“`在本教程中,过滤后的数组声明为
var filteredTableData=[String]()
,但如果这样做,则无法执行此操作`locations=filteredPreviousLocations[indexPath.row]locCells.useLocations(locations)`上次编辑后,我得到类型为“PreviousLocations”的
值没有成员“LocationNameRangeofsString”
我在
RangeofsString
之前漏掉了一个点。筛选后的数组必须与主数组具有相同的类型
func updateSearchResultsForSearchController(searchController: UISearchController)
{
  filteredPreviousLocations = previousLocation.filter { $0.locationName.rangeOfString(searchController.searchBar.text!, options: [.AnchoredSearch, .CaseInsensitiveSearch, .DiacriticInsensitiveSearch]) != nil}
  self.tableView.reloadData()
}