当前位置: 动力学知识库 > 问答 > 编程问答 >

Query Parse Array Data down into local array, and add to TableView (Swift)

问题描述:

Hi first i'd like to say (Thank you) in advance. I apologize as I feel like a huge idiot.

So I've successfully pulled an array of objectId's from parse and added it to a local array called [teamArray] See below:

func getTeamData() {

var query = PFUser.query()

query?.whereKey("objectId", equalTo: displayedTeamId)

query!.findObjectsInBackgroundWithBlock {

(objects: [AnyObject]?, error: NSError?) -> Void in

if error == nil {

// The find succeeded.

println("Successfully retrieved \(self.displayedTeamId)")

// Do something with the found objects

if let objects = objects as? [PFObject] {

for object in objects {

var team:AnyObject? = object["team"]

self.teamArray.addObjectsFromArray(team as! [PFObject])

--> successful print here: println(" I have a full \(self.teamArray)")

// code to download an image

let imageFile = object["teamImage"] as! PFFile

imageFile.getDataInBackgroundWithBlock {

(imageData: NSData?, error: NSError?) -> Void in

if error != nil {

println(error)

} else {

if let data = imageData {

self.showTeamImage.image = UIImage(data: data)

self.tableView.reloadData()

}

}

}

self.tableView.reloadData()

}

}

} else {

// Log details of the failure

println("Error: \(error!) \(error!.userInfo!)")

}

}

}

The team array is like so:

var teamArray:NSMutableArray = []

So, my goal is to take each object in the array, and add it into into the tableView cells.

I have no idea how to do this. I'll re-iterate.

  1. I want to pull down the parse array which is an array of objectId's (as Strings)

  2. I want to extract each ObjectId within the [teamArray], and add that object into it's own cell.

  3. Pull the data out of that object.

    • profile photo
    • name
    • location
    • age
    • gender

Now how do I approach this???

网友答案:

This example that I am about to show you is for iOS 9 with the new parse SDK.

You want to display a lot of information in your TableViewCell, so create a model for example struct which contains all those informations.

struct PersonalInfo
{
    var profilePhoto:UIImage!
    var name:String!
    var location:CLLocationCoordinate2D!
    var age:Int!
    var gender:String!
}
  • Put this structure outside of the ViewController class

Then instead of having an array of NSMutableArray array just create an var arrayOfData = [PersonalInfo]()

  • Your query will now look like:

    func getTeamData()
    {
    var query = PFUser.query()
    query?.whereKey("objectId", equalTo: displayedTeamId)
    query?.findObjectsInBackgroundWithBlock({ (objects:[PFObject]?, error :NSError?) -> Void in
        if error == nil
        {
            if let objects = objects
            {
                for one in objects
                {
    
                    let imageToRetreive = one["imageFromParse"] as! PFFile
                    imageToRetreive.getDataInBackgroundWithBlock({ (data :NSData?, error:NSError?) -> Void in
                        if error == nil
                        {
                            if let data = data
                            {
                              let finalImage = UIImage(data: data)
                                let name = one["username"] as! String
                                let gender = one["gender"]  as! String
                                let age = one["age"] as! Int
                                let location = one["Location"] as! PFGeoPoint
    
                                var Info = PersonalInfo()  
    
                                Info.name = name
                                Info.gender = gender
                                Info.age = age
                                Info.profilePhoto = finalImage
                                Info.location = CLLocationCoordinate2DMake(location.latitude, location.longitude)
    
                                self.arrayOfData.append(Info)
                                // <--- then reload the tableview
    
                            }
    
                        }
                    })
                }
            }
        }
      })
     }
    
    • Then in you cellForRowAtIndexPath method you will have something like: var data = arrayOfData[indexPath.row] cell.textLabel.text = data.name // <---- access everything from the array using the . method from data
网友答案:

The question needs a little more clarification. First of all, is object["team"] returning an array? or is it representing one single object? Another option going off on the answer above is to use PFSubclassing to represent your results as parse objects It could look something like this:

class Team: PFObject, PFSubclassing {
    @NSManaged var profilePhoto: PFFile?
    @NSManaged var name: String

    // The name of your class in parse
    static func parseClassName() -> String {
        return "Team"
      }

    override class func initialize() {
        struct Static {
          static var onceToken : dispatch_once_t = 0;
        }
        dispatch_once(&Static.onceToken) {
          self.registerSubclass()
        }
      }
}

This way you can perform all of the PFObject operations on your objects Eg.

let team = Team()
team.name = "Team name here"

let imageNamed = UIImage(named: "image.jpg")
let imageData = UIImageJPEGRepresentation(imageNamed, 1)
let imageFile = PFFile(data: imageData)
team.profileImage = imageFile
team.saveInBackground()
分享给朋友:
您可能感兴趣的文章:
随机阅读: