2014-09-27 140 views
0

即時通訊在Xcode 6中做一個簡單的項目,我想在tableviewcontroller中添加搜索欄,但有些東西不適合我。林本教程做 http://www.raywenderlich.com/76519/add-table-view-search-swift在表視圖中的搜索欄Swift

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if tableView == self.searchDisplayController!.searchResultsTableView { 
     return self.filteredProgramy.count 
    } else { 
     return self.programy.count 
    } 
} 

這裏即時得到錯誤「致命錯誤:意外發現零而展開的可選值」。 idk爲什麼。完整的代碼在

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if tableView == self.searchDisplayController!.searchResultsTableView { 
     return self.filteredProgramy.count 
    } else { 
     return self.programy.count 
    } 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell 

    var program : Program 

    if tableView == self.searchDisplayController!.searchResultsTableView { 
     program = filteredProgramy[indexPath.row] 
    } else { 
     program = programy[indexPath.row] 
    } 


func filterContentForSearchText(searchText: String) { 
    // Filter the array using the filter method 
    var scope = String() 
    self.filteredProgramy = self.programy.filter({(program: Program) -> Bool in 
     let categoryMatch = (scope == "All") || (program.category == scope) 
     let stringMatch = program.name.rangeOfString(searchText) 
     return categoryMatch && (stringMatch != nil) 
    }) 
} 

func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool { 
    self.filterContentForSearchText(searchString) 
    return true 
} 

func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchScope searchOption: Int) -> Bool { 
    self.filterContentForSearchText(self.searchDisplayController!.searchBar.text) 
    return true 
} 

}

+1

該錯誤無關特別是在搜索欄中做什麼,並且要了解如何正確使用optionals,你會得到哪一行錯誤?你可能會強制打開一個可選值並得到nil(如錯誤信息所述) – 2014-09-27 01:16:15

+0

這裏。「如果tableView == self.searchDisplayController!.searchResultsTableView {」 – patrikbelis 2014-09-28 17:42:10

+0

你有沒有得到這個整理? – 2014-09-29 20:44:47

回答

1

self.searchDisplayController爲零。

我剛剛下載了教程的示例代碼(你應該這樣做),我發現作者在他們的nib文件中有一個「搜索顯示控制器」。檢查你的筆尖文件,並確保Candy Search控制器正確連接。

它應該是這樣的:

enter image description here

爲了達到這樣的圖像用右鍵單擊的.xib文件Search Display Controller對象。請注意,在我的圖像中,「參考插座」在searchDisplayControllerCandySearch之間有連接。這就是你所缺少的。

要創建從CandySearch控制器到`搜索顯示控制器」當你放開鼠標的連接Ctrl拖動您將看到:

enter image description here

點擊searchDisplayController,你應該是好去。

最後,你應該閱讀了關於自選斯威夫特是如何工作的,以幫助您瞭解在未來這樣的問題:

https://developer.apple.com/librarY/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_456

-1

我有一個類似的問題,發現以下工作。在你的代碼中的「細胞」變量是零,因爲當你已經設置的行數,實際電池對象還沒有被創建(系細胞=的UITableView(..下面)

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell : UITableViewCell 
    var player : Player 

    if self.searchDisplayController!.active { 
     var searchCell: AnyObject? = self.tableView.dequeueReusableCellWithIdentifier("Cell") 

     if searchCell == nil { 
      cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell") 
     } else { 
      cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell 
     }    

     player = self.filteredPlayers[indexPath.row] 
    } else { 
     cell = self.tableView.dequeueReusableCellWithIdentifier(TableCellNamesEnum.PLAYER_DETAIL_CELL.rawValue, forIndexPath: indexPath) as UITableViewCell 
     cell.accessoryType = UITableViewCellAccessoryType.Checkmark 
     player = self.selectedPlayers[indexPath.row] 
    } 

    cell.textLabel!.text = "\(player.firstName) \(player.lastName)" 

    return cell 
} 
+0

你爲什麼要混合使用'dequeueReusableCellWithIdentifier(_ :)'和'dequeueReusableCellWithIdentifier(_:forIndexPath :)'?爲什麼你把'searchCell'轉換成'AnyObject'?並且不需要將'dequeueReusableCellWithIdentifier(_ :)'的返回類型轉換爲'UITableViewCell'。 – vikingosegundo 2017-01-20 10:17:56

+0

主要是因爲那是在特定版本的xCode的示例教程中使用的代碼:) – as21 2017-01-30 03:40:21