2016-11-14 73 views
3

我想搜索名稱鍵的練習詞典,然後在表格視圖中顯示過濾結果。我使用這個功能過濾數組並在表視圖中顯示?

​​

變量:

var exercises = [Exercise]() 
var filtered: [NSMutableArray] = [] 
var searchActive: Bool = false 

在搜索功能我得到的錯誤

Type'Exercise」無標會員

和那麼我有問題,結果是一個NSMutableArray,所以我不能設置resul牛逼的名字爲單元格文本顯示

無法將類型「的NSMutableArray」的價值在強制輸入「串」

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 
    if (searchActive){ 
     cell.textLabel?.text = filtered[indexPath.row] as String 
    } else { 
     let exercise = exercises[indexPath.row] 

     cell.textLabel!.text = exercise.name 
    } 
    return cell 
} 

這裏是我運動的字典以供參考:

final public class Exercise { 
    var id: Int 
    var descrip: String 
    var name: String 
    var muscles: [Int] 
    var equipment: [Int] 

    public init?(dictionary: [String: Any]) { 

     guard 
      let id = dictionary["id"] as? Int, 
      let descrip = dictionary["description"] as? String, 
      let name = dictionary["name"] as? String, 
      let muscles = dictionary["muscles"] as? [Int], 
      let equipment = dictionary["equipment"] as? [Int] 

      else { return nil } 

     self.id = id 
     self.descrip = descrip 
     self.name = name 
     self.muscles = muscles 
     self.equipment = equipment 

    } 

我可以通過使var過濾來修復第二個錯誤:[String] = []所以它可以用作單元格標題,但是這並不能解決第一個錯誤,我不知道是否正確的方式去解決它?

回答

2

filtered也類型[Exercise],你需要對其進行過濾喜歡。

var filtered = [Exercise]() 

self.filtered = exercises.filter { $0.name == searchText } 

這裏$0Exercise對象的類型,所以你需要使用$0.name訪問其屬性名稱。

編輯:如果你想filtered與惟一名稱類型的[String],那麼你可以根據需要同時使用mapfilter這個樣子。

self.filtered = exercises.filter { $0.name == searchText }.map { $0.name } 

OR

self.filtered = exercises.map { $0.name }.filter { $0 == searchText } 

或直接使用filterMap作爲@dfri建議。

self.filtered = exercises.flatMap{ $0.name == searchText ? $0.name : nil } 
+0

感謝這個,但是如果我設置過濾,也= [運動]()怎麼了我的意思將它映射到單元名稱時,它不再是一個字符串?看到我的代碼部分cell.textLabel?.text = filtered [indexPath.row] as String – infernouk

+0

@infernouk檢查編輯答案:) –

+0

我試過這個,它不喜歡它,我是否也需要編輯變量或者只是插入規定的代碼?錯誤無法將類型'String'的值轉換爲結束結果類型'Exercise'並在表格單元格名稱語句中:無法將強制類型'Exercise'的值轉換爲鍵入'String' – infernouk

0

這裏的問題是$0["name"] == searchText$0在這種情況下是Excersise類型,您可以將它稱爲數組。這條線應該是這樣的:

let filtered = exercises.filter { $0.name == searchText }

3

我昨天告訴你使用自定義的類時忘記字典語義
請勿使用密鑰腳本!

正如在其他的答案中提到必須聲明filtered也爲[Exercise]

var filtered = [Exercise]() 

,如果你也想搜索部分字符串使用過濾

self.filtered = exercises.filter { $0.name == searchText } 

然而

self.filtered = exercises.filter {$0.name.range(of: searchText, options: [.anchored, .caseInsensitive, .diacriticInsensitive]) != nil } 

但是

因爲這兩個過濾和未過濾的陣列型[Exercise]你必須改變cellForRowAtIndexPath這樣

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 
    let exercise : Exercise 
    if (searchActive){ 
     exercise = filtered[indexPath.row] 
    } else { 
     exercise = exercises[indexPath.row] 
    } 
    cell.textLabel!.text = exercise.name 
    return cell 
} 
+0

謝謝我試過這個,因爲它看起來更乾淨,保持過濾器相同的類型,沒有錯誤只是即時仍然得到這個奇怪的崩潰時,在搜索欄中鍵入一個字母,如Nirav的迴應評論 – infernouk

+0

你使用'UISearchController'嗎?如果是使用'searchResultController.isActive'來檢查搜索狀態而不是自定義布爾變量。 – vadian

+0

所以我需要聲明searchResultController作爲一個變量?只是將其換爲searchResultController.isActive會導致未解決的標識符錯誤。我已經讓searchController = UISearchController(searchResultsController:nil) – infernouk

相關問題