我收到错误:"致命错误:数组索引超出范围"从我的单元格中的解析中检索数据。而我奇怪的是我在不同的类中使用相同的代码,但我可以检索数据(但不准确)
它在这里给我错误(在cellForRowAtIndexPath方法中) self.profileFile [indexPath.row] .getDataInBackgroundWithBlock({(imageData,error) - > Void in
我有5个对象,所以如果删除这行,我可以打印5个对象。
我不知道为什么它会在这里给我错误。
var user = PFUser.currentUser()
var friends = [String]()
var profileFile = [PFFile]()
override func viewDidLoad() {
super.viewDidLoad()
//friends
var query = PFQuery(className:"Requests")
//query.orderByDescending("requestBy")
query.whereKeyExists("requestTo")
query.whereKey("requestBy", equalTo: PFUser.currentUser()!.username!)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Friends recieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
println(object)
self.friends.append(object["requestTo"] as! String)
println(self.friends)
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
self.tableView.reloadData()
}
}
override func viewDidAppear(animated: Bool) {
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return friends.count
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 65
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell1 = tableView.dequeueReusableCellWithIdentifier("friendCell") as! friendsTableViewCell
myCell1.friendLabel.text = friends[indexPath.row]
var query = PFQuery(className: "image")
query.whereKeyExists("imageFile")
query.whereKey("userName", equalTo : myCell1.friendLabel.text!)
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
for object in objects!{
self.profileFile.append(object["imageFile"] as! PFFile)
self.profileFile[indexPath.row].getDataInBackgroundWithBlock({ (imageData, error) -> Void in
if error == nil {
let image = UIImage(data: imageData!)
myCell1.profielPic.image = image
}
})
}
}
self.tableView.reloadData()
}
//println(indexPath.row)
return myCell1
}
}