UISearchBar for tableView in Swift 中具有多个原型单元格



我需要为我的动态表视图实现一个搜索栏。在 Swift 中的搜索栏 http://www.raywenderlich.com/76519/add-table-view-search-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") as UITableViewCell

您需要为正在使用的每个标识符的表视图单元格注册一个类或 nib 文件。一个好地方是viewDidLoad

if let tableView = self.searchDisplayController?.searchResultsTableView {
  tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier:"songCell")
  tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier:"movieCell")
}

相关内容

最新更新