0

我正在尝试在表格视图中使用搜索功能。数据会按预期更改,但是当我点击搜索后生成的单元格时,它不会带我进入下一个视图。它在 selectedIndex var 上的这段代码上崩溃。

if self.tableView == self.searchDisplayController!.searchResultsTableView {
        var selectedIndex = self.tableView.indexPathForSelectedRow()!.row
        selectedTimeSlot = filteredTimeSlots[selectedIndex]
    } else {
        var selectedIndex = self.tableView.indexPathForSelectedRow()!.row
        selectedTimeSlot = timeSlots[selectedIndex]
    }

这是我的全部代码

import UIKit
import Alamofire

class ClockEditTableViewController: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate {

    // Contains all time slots
    var timeSlots: [TimeSlot] = [TimeSlot]()

    // Contains the search results
    var filteredTimeSlots: [TimeSlot] = [TimeSlot]()

    override func viewDidLoad() {

//        timeSlots = DataHelper.getTimeSlots()
        Alamofire.request(.GET, "http://wouterhabets.com/acts.json")
            .response { (request, response, data, error) in
                if error == nil {
                    self.timeSlots = DataHelper.getTimeSlots(data as NSData)
                    self.tableView.reloadData()
                } else {
                    self.timeSlots = DataHelper.getTimeSlots()
                    self.tableView.reloadData()
                }
        }

        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var actDetailViewController: ActDetailsViewController = segue.destinationViewController as ActDetailsViewController
        var selectedTimeSlot: TimeSlot

        if self.tableView == self.searchDisplayController!.searchResultsTableView {
            var selectedIndex = self.tableView.indexPathForSelectedRow()!.row
            selectedTimeSlot = filteredTimeSlots[selectedIndex]
        } else {
            var selectedIndex = self.tableView.indexPathForSelectedRow()!.row
            selectedTimeSlot = timeSlots[selectedIndex]
        }

        actDetailViewController.timeSlot = selectedTimeSlot
    }

    // Return the amount of cells to display
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var count: Int

        // Check if the user is searching
        if tableView == self.searchDisplayController!.searchResultsTableView {
            count = filteredTimeSlots.count
        } else {
            count = timeSlots.count
        }

        return count
    }

    // Fill cells with data
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCellWithIdentifier("actCell", forIndexPath: indexPath) as UITableViewCell

        var timeSlot: TimeSlot

        // Check if the user is searching
        if tableView == self.searchDisplayController!.searchResultsTableView {
            timeSlot = filteredTimeSlots[indexPath.row]
        } else {
            timeSlot = timeSlots[indexPath.row]
        }

        cell.textLabel?.text = timeSlot.act?.title

        return cell
    }

    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
    }

    func filterContentForSearchText(searchText: String) {
        self.filteredTimeSlots = self.timeSlots.filter({( timeSlot: TimeSlot) -> Bool in

            // Optional scope search code
            // let categoryMatch = (scope == "All") || (band.category == scope)
            let searchText = searchText.lowercaseString

            let stringMatch = timeSlot.act?.title?.lowercaseString.rangeOfString(searchText)
            return (stringMatch != nil) // && categoryMatch
        })
    }

}

有谁知道我做错了什么以及我应该做些什么来解决这个问题

4

1 回答 1

0

self.tableView是主表视图,所以

self.tableView == self.searchDisplayController!.searchResultsTableView

永远不会是真的

您应该检查活动 tableView 是主 tableView 还是搜索 tableView

if self.searchDisplayController.active {
    // get the index path from search table ... 
} else {
    // get the index path from main table ...
}

这也在这里得到了回答。 searchDisplayController、UITableView、Core Data 和 Swift

另外,在使用感叹号时要小心

self.tableView.indexPathForSelectedRow()!

因为这可能会返回 nil,如果它返回 nil,您将收到运行时错误

于 2015-02-04T15:54:36.340 回答