在Swift中的UITableViewController中搜索显示控制器错误

时间:2014-07-16 08:14:37

标签: uitableview swift ios8 uisearchbardisplaycontrol

UITableViewControllerSearch Bar and Search Display Controller。当我尝试在搜索中输入内容并且我的代码更改数组arrayForSearchTable中的数据时,应用程序会崩溃。该数组需要在表格中显示结果。错误:

*** Assertion failure in -[UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-3262.1/UITableView.m:6101

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier SearchCityCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

我做了testArrayForSearchTable的验证数组arrayForSearchTable。数组的内容显示 - 数据正确,通常显示:

************
utfURL: http://autocomplete.wunderground.com/aq?query=s
************
City: Sagamihara, Japan, Country: JP, ZMW: 00000.6.47680`
City: Sacramento, California, Country: US, ZMW: 94203.1.99999
City: Safi, Morocco, Country: MA, ZMW: 00000.1.60185
..................

有什么问题?

我的代码:

import UIKit

class AddFavCityTableViewController: UITableViewController {

var arrayForSearchTable: [Dictionary<String, String>] = []
var myAPI:APIController = APIController()

override func viewDidLoad() {
    super.viewDidLoad()
    println("Current count in result array: \(arrayForSearchTable.count)")
}

override func viewDidAppear(animated: Bool) {
    if searchDisplayController.active {
        searchDisplayController.searchResultsTableView.reloadData()
    } else {
        //reloadTable("")
        self.tableView.reloadData()
    }
}

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


override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
    return 1
}

override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    return arrayForSearchTable.count
}

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

    var cell = tableView.dequeueReusableCellWithIdentifier("SearchCityCell", forIndexPath: indexPath) as UITableViewCell

    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "SearchCityCell")
    }

    let cellCity: String? = (arrayForSearchTable[indexPath.row] as NSDictionary).valueForKey("Name") as? String
    cell.textLabel.text = cellCity

    let cellCountry: String? = (arrayForSearchTable[indexPath.row] as NSDictionary).valueForKey("Country") as? String
    cell.detailTextLabel.text = cellCountry

    return cell
}

func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {

    reloadTable(searchString)
    return true
}

func reloadTable(searchString: String) {

    var urlPath: String = "http://autocomplete.wunderground.com/aq?query=\(searchString)"
    println("************")
    println("urlPath: \(urlPath)")

    var receivedCitiesArray: NSArray = ((myAPI.jsonParsingWeather(urlPath)).valueForKey("RESULTS")?) as NSArray

    var cityInSearchList = [String: String]()

    // Test array, copy of arrayForSearchTable for cheking loop
    var testArrayForSearchTable: [Dictionary<String, String>] = []

    for tempArray in receivedCitiesArray {
        var tempDict = tempArray as NSDictionary
        cityInSearchList["Country"] = tempDict.valueForKey("c") as String!
        cityInSearchList["Name"] = tempDict.valueForKey("name") as String!
        cityInSearchList["ZMW"]  = tempDict.valueForKey("zmw") as String!
        testArrayForSearchTable += cityInSearchList
        arrayForSearchTable += cityInSearchList
    }


    //Loop for cheking result array
    println("************")
    for tempArray2 in testArrayForSearchTable {
        var testCity = (tempArray2 as NSDictionary).valueForKey("Name") as String
        var testCountry = (tempArray2 as NSDictionary).valueForKey("Country") as String
        var testZMW = (tempArray2 as NSDictionary).valueForKey("ZMW") as String
        println("City: \(testCity), Country: \(testCountry), ZMW: \(testZMW)")
    }


    self.tableView.reloadData()

}
}

1 个答案:

答案 0 :(得分:0)

解决!相反

var cell = tableView.dequeueReusableCellWithIdentifier("SearchCityCell", forIndexPath: indexPath) as UITableViewCell

写:

var cell = tableView.dequeueReusableCellWithIdentifier("SearchCityCell") as UITableViewCell!