fatal error: Array index out of range with Parse.com
fatal error: Array index out of range with Parse.com
我有一个 Swift 2 和 Parse.com 的应用程序。我添加了 UISearchController,当我使用它时,应用程序崩溃了。并且显示错误 "fatal error: Array index out of range"
。
当我在搜索栏中触摸时,字符串 "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
}
}
我有一个 Swift 2 和 Parse.com 的应用程序。我添加了 UISearchController,当我使用它时,应用程序崩溃了。并且显示错误 "fatal error: Array index out of range"
。
当我在搜索栏中触摸时,字符串 "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
}
}