Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Swift中的UITableViewController中搜索显示控制器错误_Uitableview_Swift_Ios8_Uisearchbardisplaycontrol - Fatal编程技术网

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

在Swift中的UITableViewController中搜索显示控制器错误,uitableview,swift,ios8,uisearchbardisplaycontrol,Uitableview,Swift,Ios8,Uisearchbardisplaycontrol,我有UITableViewController有搜索栏和搜索显示控制器。当我尝试在搜索中键入某些内容,并在arrayarrayForSearchTable中更改代码数据时,应用程序会崩溃。此数组需要在表中显示结果。错误: ***在-[UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:],/SourceCache/UIKit_Sim/UIKit-3262.1/UITableView.m:6101中断言失败

我有
UITableViewController
搜索栏和搜索显示控制器
。当我尝试在搜索中键入某些内容,并在array
arrayForSearchTable
中更改代码数据时,应用程序会崩溃。此数组需要在表中显示结果。错误:

***在-[UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:],/SourceCache/UIKit_Sim/UIKit-3262.1/UITableView.m:6101中断言失败

***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法将标识符为SearchCityCell的单元格出列-必须为标识符注册nib或类,或在情节提要中连接原型单元格”

我验证了数组
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()

}
}
导入UIKit
类AddFavCityTableViewController:UITableViewController{
var arrayForSearchTable:[字典]=[]
var myAPI:APIController=APIController()
重写func viewDidLoad(){
super.viewDidLoad()
println(“结果数组中的当前计数:\(arrayForSearchTable.count)”)
}
覆盖功能视图显示(动画:Bool){
如果searchDisplayController.active{
searchDisplayController.searchResultsTableView.reloadData()
}否则{
//可重新加载(“”)
self.tableView.reloadData()
}
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
}
覆盖func numberOfSectionsInTableView(tableView:UITableView!)->Int{
返回1
}
重写func tableView(tableView:UITableView!,numberofrowsinssection:Int)->Int{
返回arrayForSearchTable.count
}
重写func tableView(tableView:UITableView!,cellForRowAtIndexPath:nsindepath!)->UITableViewCell{
var cell=tableView.dequeueReusableCellWithIdentifier(“SearchCityCell”,forIndexPath:indexPath)作为UITableViewCell
如果单元格==nil{
单元格=UITableViewCell(样式:UITableViewCellStyle.Subtitle,reuseIdentifier:“SearchCityCell”)
}
让cellCity:String?=(arrayForSearchTable[indexPath.row]作为NSDictionary)。valueForKey(“名称”)作为?String
cell.textlab.text=单元格城市
让cellCountry:String?=(arrayForSearchTable[indexPath.row]作为NSDictionary)。valueForKey(“国家”)作为?字符串
cell.detailTextLabel.text=cellCountry
返回单元
}
func searchDisplayController(控制器:UISearchDisplayController!,应重新加载搜索字符串的表searchString:String!)->Bool{
重载表(搜索字符串)
返回真值
}
func重载表(searchString:String){
变量urlPath:字符串=”http://autocomplete.wunderground.com/aq?query=\(搜索字符串)”
println(“*************”)
println(“urlPath:\(urlPath)”)
var receivedCitiesArray:NSArray=((myAPI.jsonParsingWeather(urlPath)).valueForKey(“结果”)?)作为NSArray
var citySearchList=[String:String]()
//测试阵列,用于检查循环的阵列搜索表副本
var testArrayForSearchTable:[字典]=[]
对于receivedCitiesArray中的tempArray{
var tempDict=tempArray作为NSDictionary
城市搜索列表[“国家”]=tempDict.valueForKey(“c”)作为字符串!
CitySearchList[“Name”]=tempDict.valueForKey(“Name”)作为字符串!
CitySearchList[“ZMW”]=tempDict.valueForKey(“ZMW”)作为字符串!
testArrayForSearchTable+=城市搜索列表
arrayForSearchTable+=城市搜索列表
}
//检查结果数组的循环
println(“*************”)
对于testArrayForSearchTable中的tempArray2{
var testCity=(tempArray2作为NSDictionary)。valueForKey(“Name”)作为字符串
var testCountry=(tempArray2作为NSDictionary)。valueForKey(“国家”)作为字符串
var testZMW=(tempArray2作为NSDictionary)。valueForKey(“ZMW”)作为字符串
println(“城市:\(testCity),国家:\(testCountry),ZMW:\(testZMW)”)
}
self.tableView.reloadData()
}
}
已解决!反而

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

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

我想我现在也有同样的问题……但是如果我使用您编写的第二个方法(没有indexPath),我如何访问要在数据源数组中显示的对象?