2015-10-18 41 views
0

我有一個表視圖,它從我的服務器獲取json數據。 現在我想用SearchContoller添加搜索欄。細胞(自定義)搜索前正確顯示:搜索控制器 - 與模型使用NSPredicate的問題

enter image description here

,但是當我開始打字什麼也看不見了。下面是代碼:

模型

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]` 

This教程在這個解釋,我得到 Cannot assign a value of type '[String]' to a value of type '[PreviousLocations]'

回答

2

類型的屬性被明確定義爲[PreviousLocations]錯誤,所以你不需要投除過濾器行外的類型。格式字符串應該是locationName == %@

func updateSearchResultsForSearchController(searchController: UISearchController) 
{ 
    let searchPredicate = NSPredicate(format: "locationName == %@", searchController.searchBar.text!) 
    filteredPreviousLocations = (previousLocation as NSArray).filteredArrayUsingPredicate(searchPredicate) 
    self.tableView.reloadData() 
} 

或者,您可以使用Swift的filter函數進行過濾。

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() 
} 
+0

@在第一個解決方案,我得到'型的價值「[PreviousLocations]」沒有成員「filteredArrayUsingPredicate''和第二我STIL得到一個空表時,我開始輸入 – mat

+0

感謝,但現在'C annot將類型'[AnyObject]'的值賦值爲類型'[PreviousLocations]'的值'在教程中,過濾的數組被聲明爲'var filteredTableData = [String]()',但如果我這樣做那麼我不能做這'位置= filteredPreviousLocations [indexPath.row] locCells.useLocations(位置)' – mat

+0

您最後一次編輯後我得到'價值的類型'PreviousLocations'沒有成員'locationNamerangeOfString'' – mat