Friday, June 12, 2015

How to Custom Parse PFTableViewCell using Swift

Ok, let say you want to load some data from Parse into your custom made PFTableViewCell, below is the step by step

Step1: Drag a TableViewController into your main scene

Step2: Select your prototype cells, then change the style to "Custom", drag 4 label into your prototype cell and give them tag "10", "20", "30", "40"


Step3: Change the Class of your prototype cell to "PFTableViewCell", because you need to load data from Parse, so you need this


Step4: Create a new File -> Cocoa Touch Class
Class Name: MyTableViewController
SubClassName: PFQueryTableViewController

Then on the main scene, click your table view controller, set your class to "MyTableViewController"


Step5: Open your MyTableViewController.swift

put in the below code

////////////////////////////////////////////////////////////////////////////////////////////////////////

import UIKit
import Parse
import ParseUI

class MyTableViewController: PFQueryTableViewController {

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 = "Request"
        self.textKey = "item_wanted"
        self.pullToRefreshEnabled = true
        self.paginationEnabled = true 
       self.objectsPerPage = 25   
  
    }

 // Define the query that will provide the data for the table view
    override func queryForTable() -> PFQuery {
        var query = PFQuery(className: "Request")    
        query.includeKey("requester")

        return query
    }

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
        
        var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as PFTableViewCell!
        if cell == nil {
            cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
        }


        if let mysubtitle = object?["email"] as? String {
                (cell.contentView.viewWithTag(10) as UILabel).text = mysubtitle
         }
        if let mysubtitle2 = object?["item_wanted"asString {
                (cell.contentView.viewWithTag(20as UILabel).text = mysubtitle2
         }
        if let mysubtitle3 = object?["city"asString {
                (cell.contentView.viewWithTag(30as UILabel).text = mysubtitle3
         }
        if let mysubtitle4 = object?["zipcode"asString {
                (cell.contentView.viewWithTag(40as UILabel).text = mysubtitle4
         }
        
        return cell
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 50
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

/////////////////////////////////////////////////////////////////////////////////
Code Explanation:
When you are using subclass of PFQueryTableViewController , you need to populate the below function

1.  required init(coder aDecoder: NSCoder)
- this is to initialize the Class, you need to tell which "Table" you want to request from Parse, my example I request to query the table "Request"

2. override func queryForTable() -> PFQuery {
- this is to write your query

3. override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {

- this is to specify what need to display on your cell 
- since we already tag our label with "10", "20", "30", "40" so we can populate data from Parse into the cell label by doing the coding below, my column "item_wanted" populate into the label with tag "10"

if let mysubtitle = object?["item_wanted"asString {
                (cell.contentView.viewWithTag(10as UILabel).text = mysubtitle
         }


here is my dummy data i loaded from Parse

hopefully it can hep you in some way

No comments:

Post a Comment