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

Scroll UICollectionView to bottom

I would like to scroll the UICollectionView to the bottom so the last item is in the view. I have tried to use scrollToItemAtIndexPath but it does not seem to be working. I want this to happen after I have completed a query with Parse.com

Thanks

        var query = PFQuery(className:"Chat")
        //        query.whereKey("user", equalTo:currentUser)
        query.whereKey("rideId", equalTo:currentObjectId)
        query.orderByDescending("createdAt")
        query.includeKey("user")

        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]!, error: NSError!) -> Void in
            if error == nil {
                // The find succeeded.
                NSLog("Successfully retrieved \(objects.count) scores.")
                // Do something with the found objects
                for object in objects {
                    NSLog("%@", object.objectId)

                    var testId = object.objectId

                    println(testId)

                    self.orderedIdArray.append(testId)

                    var message = object.objectForKey("message") as String
                    self.messageString = message
                    self.messageArray.append(self.messageString)
                    println(message)

                    var nameId = object.objectForKey("user") as PFUser
                    var username = nameId.username as String
                    self.nameString = username
                    self.namesArray.append(self.nameString)

                    println("username: \(username)")

                    self.collectionView?.reloadData()
                }

            } else {
                // Log details of the failure
                NSLog("Error: %@ %@", error, error.userInfo!)
            }
                        NSLog("Ordered: %@", self.orderedIdArray)    
        }

1 Answer

I have added this to run once the query has completed.

var item = self.collectionView(self.collectionView!, numberOfItemsInSection: 0) - 1
            var lastItemIndex = NSIndexPath(forItem: item, inSection: 0)
            self.collectionView?.scrollToItemAtIndexPath(lastItemIndex, atScrollPosition: UICollectionViewScrollPosition.Top, animated: false)

So have you found a solution to your problem?

Yes thank you.

Yes thank you.