Vinícius Soler
Vinícius Soler

Reputation: 33

Parse using in swift (Tableview)

It's the first time I move with Parse. Parse did the pod, I import the class in Bridging-Hearder and declared the Delegate (swift) the Id and the clientKey I will use. Let my doubt, what I have to do to get this data from Parse and insert them in a TableView?

Upvotes: 0

Views: 2259

Answers (1)

Mauricio Valdes
Mauricio Valdes

Reputation: 76

ok the first thing you should do is in the AppDelegate set id parse

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

    Parse.setApplicationId("ID-PARSE", clientKey: "KEY-PARSE") return true }

create a object

var message:PFObject = PFObject(className:"CLASS-NAME")
    message["NAME ROW IN YOUR DATABASE PARSE"] = messageBox.text as String
    message["User"] = PFUser.currentUser()
    message.saveInBackground()

load data parse

func loaddata()
{
    var dataParse:NSMutableArray = NSMutableArray()
    var  findDataParse:PFQuery = PFQuery(className: "NAME ROW IN YOUR DATABASE PARSE")

    findDataParse.findObjectsInBackgroundWithBlock{
        (objects:AnyObject[]!, error:NSError!)->Void in

        if (!error){
            for object:PFObject! in objects
            {
                self.dataParse.addObject(object)
            }
        }
        //// add data into array  
        let array:NSArray = self.dataParse.reverseObjectEnumerator().allObjects
        self.dataParse = array as NSMutableArray

        self.tableView.reloadData()
    }

}

now table

override func numberOfSectionsInTableView(tableView: UITableView!) -> Int
{
    return 1
}


override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
{
    return self.dataParse.count
}


override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell?
{

    let cell:CustomCell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!) as CustomCell

    let cellDataParse:PFObject = self.dataParse.objectAtIndex(indexPath!.row) as PFObject

    cell.mensajeBox.text = cellDataParse.objectForKey("NAME ROW IN YOUR DATABASE PARSE") as String return cell }

i hope this help !

Upvotes: 3

Related Questions