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

PFObject from Selected Row and Saving to Another Parse Class

I am still new to programming and I am trying to create a button that will save the currentUser and the EventID of a PFObject. I have a class in Parse called Users Attendance that relates the User with the EventID to keep track of all the events a user says they will attend. I am having trouble setting up the UIButton to save the EventID from the PFObject that is on the row selected it from the TableView. Could someone please help?

Here is the TableView setup:

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

    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! EventsTableViewCell!
    if cell == nil {
        cell = EventsTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
    }
    cell.attend.tag = indexPath.row
    cell.attend.addTarget(self, action: "attendingEvent:", forControlEvents: UIControlEvents.TouchUpInside)


    // Extract values from the PFObject to display in the table cell

    if let eventTime = object?["EventTime"] as? String {
    cell.EventTime.text = eventTime
    }

    if let eventName = object?["EventName"] as? String {
        cell.EventName.text = eventName
    }

    var initialImage = UIImage(named: "DidNotLoad")
    cell.EventImage.image = initialImage
    if let parseEventPhoto = object?["EventImage"] as? PFFile{
        cell.EventImage.file = parseEventPhoto
        cell.EventImage.loadInBackground()
    }
    return cell
}

Here is the UIButton I attempted to create:

func attendingEvent(sender: UIButton) {

    println("You tapped the button!")

    let currentUser = PFUser.currentUser()
    var getSenderInfo = sender.tag
    var selectedRowInfo = self.tableView.indexPathForSelectedRow()
    var objectFromRow = objectAtIndexPath(selectedRowInfo)

    let eventID: PFObject = objectFromRow?["EventID"] as! PFObject

    println(eventID)

    var attendance = PFObject(className: "UsersAttendance")
    attendance.setObject(currentUser!, forKey: "AttendingUser")
    attendance.setObject(eventID, forKey: "EventID")

    attendance.saveInBackgroundWithBlock {
    (succeeded: Bool, error: NSError?) -> Void in
    if error == nil {


    } else {

    }
}

}

1 Answer

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

I'm not that advanced in swift yet, but this course might help. As a warning it is old, and it is Obj-C, not Swift, but you might get some good out of it.

P.S. Have you tried Googling? Stack Overflow is one great web-site.