致命错误:数组索引超出范围与Parse.com
问题描述:
我有夫特2和Parse.com的应用程序。我添加了UISearchController,当我使用它时,应用程序崩溃。它显示了一个错误"fatal error: Array index out of range"
。致命错误:数组索引超出范围与Parse.com
有串"if var pfFiltered = dataForSearchController?[indexPath.row]" is EXC_BAD_INSTRUCTION
一个错误,当我在搜索栏中触摸。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
var searchText = searchController.searchBar.text
let cellIndentifier: String = "NewsCell"
var cell: ParseTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIndentifier) as? ParseTableViewCell
if searchController.active == false {
if (cell == nil) {
cell = ParseTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIndentifier)
}
if let pfObject = object {
//cell?.textLabel?.text = pfObject["textNews"] as? String
cell?.labelNameText?.text = pfObject["nameNews"] as? String // name
cell?.labelDataText?.text = pfObject["dateNews"] as? String // data
cell?.labelText = pfObject["textNews"] as? String
cell?.labelURLNews = pfObject["urlNews"] as? String // link on a site with news
var imageFromParse = object?.objectForKey("imageNews") as? PFFile
imageFromParse?.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
var image: UIImage! = UIImage(data: imageData!)!
cell?.imageViewCell.image = image
})
}
} else {
if var pfFiltered = dataForSearchController?[indexPath.row] {
cell?.labelNameText?.text = pfFiltered["nameNews"] as? String
cell?.labelDataText?.text = pfFiltered["dateNews"] as? String
cell?.labelText = pfFiltered["textNews"] as? String
cell?.labelURLNews = pfFiltered["urlNews"] as? String
var imageFromFilteredParse = object?.objectForKey("imageNews") as? PFFile
imageFromFilteredParse?.getDataInBackgroundWithBlock({ (imageDataTwo: NSData?, error: NSError?) -> Void in
var imageTwo: UIImage = UIImage(data: imageDataTwo!)!
cell?.imageViewCell.image = imageTwo
})
}
}
return cell
}
答
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var query: PFQuery = PFQuery(className: "NewsNow")
if self.dataForSearchController == nil {
return objects?.count ?? 0
} else {
return dataForSearchController?.count ?? 0
}
}
可以看到的是使用之前它indexPath.row'的'价值? – donjuedo
不,我没看见。 – Alexander
你为什么不查了一下indexPath.row的值是???它说:“数组索引超出范围”所以,如果你有你的数组中的5种元素和indexPath.row是10那么很明显,这是问题,不是吗? – Gruntcakes