Wyszukaj ciąg w widoku tabeli Swift


Robię prosty projekt w Xcode 6 i chcę dodać pasek wyszukiwania do kontrolera tableview, ale coś mi nie działa. Robię to zgodnie z tym samouczkiem

http://www.raywenderlich.com/7 ... swift
http://www.raywenderlich.com/7 ... swift
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.filteredProgramy.count
} else {
return self.programy.count
}
}

tutaj pojawia się błąd „błąd krytyczny: nieoczekiwanie znaleziono zero podczas rozpakowywania wartości opcjonalnej”. Nie wiem czemu. cały kod jest tutaj
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
}

}
Zaproszony:
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

self.searchDisplayController
ma wartość zero.
Właśnie pobrałem przykładowy kod samouczka (który też powinieneś zrobić) i zobaczyłem, że autor ma w pliku nib "Search Display Controller". Sprawdź plik ze wskazówkami i upewnij się, że kontroler
Candy Search
jest prawidłowo podłączony.
Powinien wyglądać mniej więcej tak:
Aby przejść do tego obrazu, kliknij prawym przyciskiem myszy obiekt
Search Display Controller
w pliku .xib. Zauważ na moim obrazie, że „Referencing Outlets” ma związek między
searchDisplayController
a
CandySearch
. Tego właśnie brakuje.
Aby utworzyć połączenie ctrl, przeciągnij z
CandySearch
do „Search Display Controller”, kiedy zwolnisz przycisk myszy, zobaczysz:
Kliknij przycisk
searchDisplayController
i powinieneś być gotowy do pracy.
Wreszcie musisz przeczytać, w jaki sposób opcje w SWIFT, aby pomóc Ci zrozumieć takie problemy w przyszłości:
https://developer.apple.com/librarY/ Prerelease/Mac/Documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html #// apple_ref/doc/uid/TP40014097-CH5-XID_456
https://developer.apple.com/li ... D_456
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Miałem podobny problem i stwierdziłem, że działają następujące. Zmienna „cell” w twoim kodzie ma wartość zero, ponieważ chociaż podałeś liczbę wierszy, rzeczywisty obiekt komórki nie został jeszcze utworzony (line cell = UITableView (.. poniżej)
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
}

Aby odpowiedzieć na pytania, Zaloguj się lub Zarejestruj się