Marcelo Pontes Machado
Marcelo Pontes Machado

Reputation: 183

Each Cell need to have a Section - Parse and Swift

I'm implementing a Feed on my App using Parse.com, basically I'm populating a UITableViewController and everything works fine, BUT, I really like the way Instagram does, seems like the Instagram have a UIView inside each cell that works like a header and that view follows the scroll till the end of cell, I tried to search about that and I'm not successful, after some research I've realized that this feature is equally a Section, so I decide to implement Sections in my querys, I've implemented the code below:

import UIKit




class FeedTableViewController: PFQueryTableViewController {

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return UIStatusBarStyle.LightContent
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    loadCollectionViewData()

}


func loadCollectionViewData() {

    // Build a parse query object
    let query = PFQuery(className:"Feed")

    // Check to see if there is a search term


    // Fetch data from the parse platform
    query.findObjectsInBackgroundWithBlock {
        (objects: [PFObject]?, error: NSError?) -> Void in

        // The find succeeded now rocess the found objects into the countries array
        if error == nil {

             print(objects!.count)
            // reload our data into the collection view


        } else {
            // Log details of the failure

    }
}
}



// Initialise the PFQueryTable tableview
override init(style: UITableViewStyle, className: String!) {
    super.init(style: style, className: className)
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!

    // Configure the PFQueryTableView
    self.parseClassName = "Feed"
    self.pullToRefreshEnabled = true
    self.paginationEnabled = false
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return objects!.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}


override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "Section \(section)"
}

//override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! FeedTableViewCell!
    if cell == nil {
        cell = FeedTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
    }
    cell.anuncerPhoto.layer.cornerRadius = cell.anuncerPhoto.frame.size.width / 2
    cell.anuncerPhoto.clipsToBounds = true

    // Extract values from the PFObject to display in the table cell
    if let nameEnglish = object?["name"] as? String {
        cell?.title?.text = nameEnglish

    }

    let thumbnail = object?["Photo"] as! PFFile
    let initialThumbnail = UIImage(named: "loadingImage")
    cell.photoImage.image = initialThumbnail
    cell.photoImage.file = thumbnail
    cell.photoImage.loadInBackground()

    return cell
}  
}

Basically I will need to have a section for each cell, Now I'm successfully have sections working for each cell, but the problem is that the querys is repeating on the first post.

In the backend I have 3 different posts, so, in the App the UItableview need to have 3 posts with different content, with the code above I'm successfully counting the number of posts to know how many section I'll need to have and I declare that I want one post per section, but the app shows 3 sections with the same first post.

Any ideas if I'm capture the correct concept of the Instagram feature and why I'm facing this problem in my querys?

Thanks.

Upvotes: 0

Views: 299

Answers (1)

Keep the original UITableViewDataSource method and retrieve the current object using the indexPath.section

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! FeedTableViewCell!
    if cell == nil {
        cell = FeedTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
    }
    cell.anuncerPhoto.layer.cornerRadius = cell.anuncerPhoto.frame.size.width / 2
    cell.anuncerPhoto.clipsToBounds = true


    let object = objects[indexPath.section]

    // Extract values from the PFObject to display in the table cell
    if let nameEnglish = object["name"] as? String {
        cell?.title?.text = nameEnglish

    }

    let thumbnail = object["Photo"] as! PFFile
    let initialThumbnail = UIImage(named: "loadingImage")
    cell.photoImage.image = initialThumbnail
    cell.photoImage.file = thumbnail
    cell.photoImage.loadInBackground()

    return cell
}  

Upvotes: 1

Related Questions