Reputation: 1413
I am attempting to populate a table view with rows of friends. When I run the code, it sometimes works and sometimes doesn't. When it doesn't, it shows the following error:
fatal error: Array index out of range
The code for the class is here:
import UIKit
import Parse
import ParseUI
class ConnectionsViewController: PFQueryTableViewController {
var connectionObject = ConnectionClass()
var userObject = UserClass()
var friendsUsernamesListArray: [String] = []
var friendsInfoListArray: [PFObject] = []
func loadFriendsUsernames(completion: (result: [String]) -> Void)
{
print("1")
let query = PFQuery(className:"FriendsConnections")
query.whereKey("appUsername", equalTo:PFUser.currentUser()!["appUsername"])
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil{
self.friendsUsernamesListArray = objects![0]["friendsList"] as! NSArray as! [String]
completion(result: self.friendsUsernamesListArray)
}else{
print(error)
}
}
}
func loadFriendsInfo()
{
print("2")
let query : PFQuery = PFUser.query()!
query.whereKey("appUsername", containedIn: self.friendsUsernamesListArray)
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil{
for item in objects! {
self.friendsInfoListArray.append(item)
}
}else{
print(error)
}
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
print("3")
}
override func queryForTable() -> PFQuery {
print("4")
self.loadFriendsUsernames({ (result)->Void in
print("Inside Completion ")
if(result.count > 0)
{
print("Completion - Array not empty")
self.loadFriendsInfo()
}else{
print("Completion - Array empty")
}
})
let query = PFQuery(className: "FriendsConnections")
query.cachePolicy = .NetworkOnly
query.orderByAscending("createdAt")
return query
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
print("5")
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ConnectionTableCell
//print("Array Content: \(friendsInfoListArray)")
if(indexPath.row >= 0)
{
cell.connectionNameLabel.text = self.friendsInfoListArray[indexPath.row]["name"] as? String
}else{
print("Something going wrong populating the cell")
}
return cell
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
print("6")
if indexPath.row + 1 > self.objects?.count
{
return 44
}
let height = super.tableView(tableView, heightForRowAtIndexPath: indexPath)
return height
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("7")
/*Get all data of friend clicked and pass to next view*/
self.userObject.name = friendsInfoListArray[indexPath.row]["name"] as? String
self.userObject.username = friendsInfoListArray[indexPath.row]["appUsername"] as? String
self.userObject.email = friendsInfoListArray[indexPath.row]["email"] as? String
self.userObject.mobile = friendsInfoListArray[indexPath.row]["mobile"] as? String
let tempWorkAddress = friendsInfoListArray[indexPath.row]["workAddress"] as! NSArray as! [String]
let tempHomeAddress = friendsInfoListArray[indexPath.row]["homeAddress"] as! NSArray as! [String]
/*Handle addresses where empty*/
if(tempHomeAddress.isEmpty || tempWorkAddress.isEmpty)
{
self.userObject.homeAddress.append("")
self.userObject.homeAddress.append("")
self.userObject.workAddress.append("")
self.userObject.workAddress.append("")
}else{
self.userObject.homeAddress = friendsInfoListArray[indexPath.row]["homeAddress"] as! NSArray as! [String]
self.userObject.workAddress = friendsInfoListArray[indexPath.row]["workAddress"] as! NSArray as! [String]
}
//to get the image file
if let userImageFile = friendsInfoListArray[indexPath.row]["ProfilePic"] as? PFFile {
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
self.userObject.userProfilePic = UIImage(data:imageData)
}
}else{
self.userObject.userProfilePic = nil
print(error)
}
}
}else{
self.userObject.userProfilePic = nil
}
self.performSegueWithIdentifier("showConnectionDetailsSegue", sender: self)
}
/*override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.friendsUsernamesListArray.count
}*/
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
print("8")
if (segue.identifier == "showConnectionDetailsSegue") {
// pass data to next view
print(segue.description)
let destinationVC = segue.destinationViewController as! FriendDetailViewController
destinationVC.userObject = self.userObject;
}
}
}
The debugger when the execution fails highlight this line:
cell.connectionNameLabel.text = self.friendsInfoListArray[indexPath.row]["name"] as? String
Now I added print statements with numbers to see the flow, and it is as following:
4
1
3
Inside Completion
Completion - Array not empty
2
6
6
5
6
fatal error: Array index out of range
Any help will be appreciated since I am on this problem for couple of days without a solution.
Upvotes: 0
Views: 2097
Reputation: 9
in the number of sections method , you need to do this :
return array.count
thats it.
Upvotes: -1
Reputation: 15512
Look at this method
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell?
And start debugging, because you method trying to take item that is out of array.
Example:
var myArray = [0,1,2,3];
myArray[0] // 0
In your case:
var myArray = [0,1,2,3];
myArray[4] // Error!
Small help issue for you:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
print("5")
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ConnectionTableCell
//print("Array Content: \(friendsInfoListArray)")
if (self.friendsInfoListArray.count < indexPath.row) {
print("PROBLEM")
}
if(indexPath.row >= 0)
{
cell.connectionNameLabel.text = self.friendsInfoListArray[indexPath.row]["name"] as? String
}else{
print("Something going wrong populating the cell")
}
return cell
}
Upvotes: 1