我需要为动态tableView实现一个搜索栏。在http://www.raywenderlich.com/76519/add-table-view-search-swift有一个示例,用于Swift中的搜索栏。该示例仅显示如何为一个原型单元执行此操作。我有多个。
我尝试用以下方法实现此目的:
class MediaItem{
var name: String
init(name: String){
self.name = name
}
}
class Movie: MediaItem{
var director: String
init(name:String, director: String){
self.director = director
super.init(name: name)
}
}
class Song: MediaItem{
var artist: String
init(name: String, artist: String){
self.artist = artist
super.init(name: name)
}
//
// ItemsTableViewController.swift
// SearchBarTest1
//
// Created by Josh Kallus on 12/7/14.
// Copyright (c) 2014 JMKLABS. All rights reserved.
//
import UIKit
class ItemsTableViewController: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate {
var library = [MediaItem]()
var filteredLibrary = [MediaItem]()
override func viewDidLoad() {
super.viewDidLoad()
self.library = [
Movie(name: "Casablanca", director: "Michael Curtiz"),
Song(name: "Blue Suede Shoes", artist: "Elvis Presley"),
Movie(name: "Citizen Kane", director: "Orson Welles"),
Song(name: "The One And Only", artist: "Chesney Hawkes"),
Song(name: "Never Gonna Give You Up", artist: "Rick Astley")]
self.tableView.reloadData()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
func filterContentForSearchText(searchText: String){
self.filteredLibrary = self.library.filter({(mediaItem: MediaItem) -> Bool in
let stringMatch = mediaItem.name.rangeOfString(searchText)
return (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
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.filteredLibrary.count
} else {
return self.library.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell
// Configure the cell...
if tableView == self.searchDisplayController!.searchResultsTableView{
//let searchText = self.searchDisplayController!.searchBar.text
//filterContentForSearchText(searchText)
if let song = filteredLibrary[indexPath.row] as? Song{
cell = tableView.dequeueReusableCellWithIdentifier("songCell") as UITableViewCell
//cell = self.searchDisplayController.searchResultsTableView.dequeueReusableCellWithIdentifier("songCell") as UITableViewCell
cell.textLabel?.text = song.name
cell.detailTextLabel?.text = song.artist
}
else{
let movie = filteredLibrary[indexPath.row] as Movie
cell = tableView.dequeueReusableCellWithIdentifier("movieCell") as UITableViewCell
//cell = self.searchDisplayController!.searchResultsTableView.dequeueReusableCellWithIdentifier("movieCell") as UITableViewCell
cell.textLabel?.text = movie.name
cell.detailTextLabel?.text = movie.director
}
}
else{
if let song = library[indexPath.row] as? Song{
cell = tableView.dequeueReusableCellWithIdentifier("songCell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = song.name
cell.detailTextLabel?.text = song.artist
}
else{
let movie = library[indexPath.row] as Movie
cell = tableView.dequeueReusableCellWithIdentifier("movieCell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = movie.name
cell.detailTextLabel?.text = movie.director
}
}
return cell
}
目前我收到了错误"致命错误:在展开“可选”值时出乎意料地发现了nil"
在行:cell = tableView.dequeueReusableCellWithIdentifier(" songCell")作为UITableViewCell
答案 0 :(得分:6)