0

im using a gesture recognizer to increment votes in my backend and then using that to update the page and each cell. I am trying to link the image with the objectId as to having a way to update each images votes. issues keep coming up with my updateVote function because i cant seem to retrieve the objectId associated with the image from the backend. heres my code for the update vote and the gesture Recognizer:

 var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    myCell.postedImage.userInteractionEnabled = true;
    myCell.postedImage.addGestureRecognizer(swipeRight)

    var swipeLeft = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Left
    myCell.postedImage.userInteractionEnabled = true;
    myCell.postedImage.addGestureRecognizer(swipeLeft)



    return myCell

}
    func respondToSwipeGesture(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {
        switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.Right:
            updateVote(true, objectId: String())
            println("Swiped right")
            case UISwipeGestureRecognizerDirection.Left:
            updateVote(false, objectId: String())
            println("Swiped Left")
            default:
                break
            }
        }
    }

func updateVote(increment: Bool, objectId : String) {
    // Create a pointer to an object of class Posts with id 'objectId'
    var object = PFObject(withoutDataWithClassName: "Post", objectId: objectId)

    // Increment the current value of the quantity key by 1
    if increment == true {
        object.incrementKey("count", byAmount: 1)
    } else {
        object.incrementKey("count", byAmount: -1)
    }

    // Save
    object.saveInBackgroundWithBlock(nil)
}

how can i retrieve the objectId because whenever i run this i keep getting the error cannot update without specific objectId? I get the println("swiped right or left") but i cant get the vote to increment how can i make that happen?

        import UIKit
        import Parse


        class HomePage: UITableViewController {

            var images = [UIImage]()
            var titles = [String]()
            var imageFile = [PFFile]()
            var votingObjects: [PFObject] = []
            var objectIds = [""]

         override func viewDidLoad() {
            super.viewDidLoad()

            println(PFUser.currentUser())

            println(PFUser.currentUser())

            var query = PFQuery(className:"Post")
            query.orderByDescending("createdAt")
            query.limit = 15
            query.findObjectsInBackgroundWithBlock {
                (objects: [AnyObject]?, error: NSError?) -> Void in
                if error == nil  {
                    println("Successfully retrieved \(objects!.count) scores.")
                    println(objects!)
                    for objectRow in objects! {
                        let object = objectRow as! PFObject


                        if let objectIds = object["objectId"] as? String {
                            self.objectIds.append(objectIds)

                        }





                        //objectIds.append(object.objectId as? String)
                        // Adding them to the array

                        if let title = object["Title"] as? String {
                            self.titles.append(title)
                        }
                        if let imgFile = object["imageFile"] as? PFFile {
                            self.imageFile.append(imgFile)
                        }
                        self.votingObjects.append(object)
                    }
                    dispatch_async(dispatch_get_main_queue(), {
                        self.tableView.reloadData() // Updating the tableView on the main thread - important. Do some research on Grand Central Dispatch :)

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

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

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

        return titles.count

    }

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

    }

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

        var myCell:cell = self.tableView.dequeueReusableCellWithIdentifier("myCell") as! cell

        myCell.rank.text = "21"
        myCell.votes.text = votingObjects[indexPath.row]["Post"] as? String
        myCell.postDescription.text = titles[indexPath.row]

        imageFile[indexPath.row].getDataInBackgroundWithBlock { (data, error) -> Void in

            if let downloadedImage = UIImage(data: data!) {

                myCell.postedImage.image = downloadedImage

            }
        }

this is the rest of my code on that page how can i retrieve an objectId from the array?

import UIKit
import Parse
import Foundation

class cell: UITableViewCell {

    @IBOutlet weak var rank: UILabel!
    @IBOutlet weak var votes: UILabel!
    @IBOutlet weak var postedImage: UIImageView!
    @IBOutlet weak var creditLink: UIButton!
    @IBOutlet weak var addButton: UIButton!
    @IBOutlet weak var postDescription: UITextView!

}

my cell class

1 Answers1

1

You are passing an empty string as the objectID objectId: String()" so you are not selecting any row to update.

Your function should pass the objectID of the row so parse can update it

enter image description here

Icaro
  • 14,585
  • 6
  • 60
  • 75
  • what should i put instead of an empty string if i dont know which picture was swiped? i dont know the exact objectID but i know its going to have one. – DanielWolfgangTrebek Jun 02 '15 at 20:42
  • If you don't know what object you are going to inclement how will parse know? I can't really tell you how to do it as I can't see how you are retrieving your data to populate your table from parse. But what I can say is that in that moment you should be keeping the objectID to pass its value here – Icaro Jun 02 '15 at 21:54
  • i want to increment the object that is swiped i dont know what object the user will swipe but i will know when they do im asking how can i retrieve that objectId when they do swipe on it? and i added in the rest of my code in my edits. any help would be much appreciated. – DanielWolfgangTrebek Jun 02 '15 at 23:18
  • when you create your new customCell class add a field on it call objectID so when the user swipe it you can pass that value for the queue, update your code and I can help you, is "cell" the name of your class? If it is you should probabily change (and classes should always start with uppercase) – Icaro Jun 02 '15 at 23:26
  • yes cell is the name of my class and sorry i meant to make it start with a capital but messed up. And should i make it a variable i also added in the code that i have in the cell class in the edits now – DanielWolfgangTrebek Jun 02 '15 at 23:45
  • how can i add the objectID in to the cell? – DanielWolfgangTrebek Jun 04 '15 at 02:55
  • I had a look in your tables, at the moment your friends table look like a users table, you need to have a table with the columns user and friend. I recommend you have a look in the tutorials available in parse as there is a lot to be done – Icaro Jun 04 '15 at 03:03
  • i dont want a friend table i was just wondering about getting the objectID for the pics because i got it to work if i put a specific ObjectId i just cant get it to where it recognizes the image with its corresponding objectID – DanielWolfgangTrebek Jun 04 '15 at 03:08
  • is there something i can put for that? – DanielWolfgangTrebek Jun 04 '15 at 03:11
  • You are getting the obj id here "if let objectIds = object["objectId"] as? String { self.objectIds.append(objectIds)} – Icaro Jun 04 '15 at 03:11
  • my only issue is at the respondToSwipe function when i write updateVote( true, objectId: ) what do i put for objectid? because if i just write objectIds i get error cannot invoke updateVote with boolian, objectId :[string] – DanielWolfgangTrebek Jun 04 '15 at 03:28
  • you need to put the string in the table post something like "Zfj4nbh69" each row in each table has its own you need to put the one you want to update – Icaro Jun 04 '15 at 03:37
  • so i need to put the specific objectId with every image? is there another way? – DanielWolfgangTrebek Jun 04 '15 at 03:56
  • No that is how databases work, you need to indicate what row you want to change and to do so you use the objectID otherwise Parse will not know what to update – Icaro Jun 04 '15 at 04:06
  • im asking how can i have the code recognize the image with the objectId and update that objectid rows count? i cant put down every single objectId – DanielWolfgangTrebek Jun 04 '15 at 04:12
  • When you get the photo you need to pick up the objectID with it. – Icaro Jun 04 '15 at 04:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79603/discussion-between-danielwolfgangtrebek-and-icaronz). – DanielWolfgangTrebek Jun 04 '15 at 04:19