0
我有一個表視圖,它從我的服務器獲取json數據。 現在我想用SearchContoller
添加搜索欄。細胞(自定義)搜索前正確顯示:搜索控制器 - 與模型使用NSPredicate的問題
,但是當我開始打字什麼也看不見了。下面是代碼:
模型
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]'
@在第一個解決方案,我得到'型的價值「[PreviousLocations]」沒有成員「filteredArrayUsingPredicate''和第二我STIL得到一個空表時,我開始輸入 – mat
感謝,但現在'C annot將類型'[AnyObject]'的值賦值爲類型'[PreviousLocations]'的值'在教程中,過濾的數組被聲明爲'var filteredTableData = [String]()',但如果我這樣做那麼我不能做這'位置= filteredPreviousLocations [indexPath.row] locCells.useLocations(位置)' – mat
您最後一次編輯後我得到'價值的類型'PreviousLocations'沒有成員'locationNamerangeOfString'' – mat