Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Ektoras Drandakis
Ektoras Drandakis
29,580 Points

Xcode: Getting strings from parse and assign to labels(Swift)

I'm sending a textfield text with and image to parse and want to assign the text to a label in a header view and the image to the collectionview. Here's my code so far:

class Header: UICollectionReusableView {

@IBOutlet weak var groupName: UILabel!

}

class ImagesCell: UICollectionViewCell {

@IBOutlet weak var imageNew: UIImageView!

}

let reuseIdentifier1 = "Cell"

class TheNewCollectionViewController: UICollectionViewController {

@IBOutlet var imagesCollection: UICollectionView!
var allImages: NSMutableArray = []
var imagesFile: NSArray = NSArray()
var groups: NSArray = NSArray()

override func viewDidLoad() {
    super.viewDidLoad()

    queryParse()
}

// MARK: UICollectionViewDataSource

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {

    return 1
}


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return imagesFile.count
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier1, forIndexPath: indexPath) as! ImagesCell

    let imageObject = imagesFile.objectAtIndex(indexPath.row) as! PFObject

    let filedImage = imageObject.objectForKey("imageFile") as! PFFile

    filedImage.getDataInBackgroundWithBlock { (data, error) -> Void in

        if(error != nil) {

            cell.imageNew.image = UIImage(data: data!)
            self.imagesCollection.reloadData()
        }
    }

    return cell
}

func queryParse() {

    let query = PFQuery(className: "images")

    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

        if (error != nil && objects?.count != 0) {

            self.imagesFile = NSArray(array: objects!)
            self.groups = NSArray(array: objects!)
            self.imagesCollection.reloadData()
        } else {

            print("Error: \(error)")
            self.imagesCollection.reloadData()
        }
    }
}

override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

    let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "header", forIndexPath: indexPath) as! Header

    let objectQuery = PFQuery(className: "images")

    objectQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

        if error != nil {

            headerView.groupName.text = objects.objectForKey("groupName") as! String
        } else {
            print("Error: \(error)")
        }
    }

    return headerView
}

2 Answers

Rashii Henry
Rashii Henry
16,433 Points

When you run your query is it returning anything from parse? I don't see any lines where you saved any text from a textfield to Parse so i'm unsure when you're using getDataInBackground where your getting the data from.

You also may have to remove that && operator in your if statement because if there's an error in the query it will also account for the array count being 0.

Everything im making reference to is within the scope your queyParse() method.

Ektoras Drandakis
Ektoras Drandakis
29,580 Points

There's another view controller where i save the text and the image. Here it is:

  • (IBAction)shareBtn:(id)sender {

    if ([self.groupField.text isEqual: @""]) {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning!"
                                                    message:@"You need to set a group name!"
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    

    } else {

    NSData *data = UIImageJPEGRepresentation(self.imageView.image, 0.8);
    NSString *filename = [NSString stringWithFormat:@"%@.png", self.groupField.text];
    PFObject *share = [PFObject objectWithClassName:@"images"];
    PFFile *imageFile = [PFFile fileWithName:filename data:data];
    [share setObject:imageFile forKey:@"imageFile"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"Uploading";
    [hud show:YES];
    
    [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            // The image has now been uploaded to Parse. Associate it with a new object
            PFObject *share = [PFObject objectWithClassName:@"images"];
            [share setObject:imageFile forKey:@"imageFile"];
            [share setObject:self.groupField.text forKey:@"groupName"];
    
            [share saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (!error) {
    
                    NSLog(@"Saved");
                    [hud hide:YES];
                    [self performSegueWithIdentifier:@"returnHome" sender:nil];
                } else {
                    // Error
                    NSLog(@"Error: %@ %@", error, [error userInfo]);
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Error!"
                                                                    message:@"Attempting request again!"
                                                                   delegate:self
                                                          cancelButtonTitle:@"OK"
                                                          otherButtonTitles:nil];
                    [alert show];
                }
            }];
        }
    }];
    

    } }