0
votes

First, thanks for reading my question!

So, I have been running into some issues regarding getting my cell names to pass onto the next view. Right now it will pass it on but it will only display the most recently loaded cell.

Here is how it is programmed right now:

var passName = ""
var passAuthor = ""
var passLikes = 0
var passDescrip = ""

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

    // Configure the PFQueryTableView
    self.parseClassName = "Location"
    self.textKey = "Name"
    self.pullToRefreshEnabled = true
    self.paginationEnabled = true
    self.objectsPerPage = 5

}
override func queryForTable() -> PFQuery {
    let query = PFQuery(className: "Location")
    query.orderByAscending("Name")
    return query
}

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

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomPFTableViewCell!

    print("Loading Parse Database Files...")
    // Extract values from the PFObject to display in the table cell
    if let name = object?["Name"] as? String {
        cell?.nameTextLabel?.text = name
        passName = name
        print("Loading " + name)
    }
    if let author = object?["authorName"] as? String {
        cell?.authorTextLabel?.text = author
        passAuthor = author
    }
    if let likes = object?["Likes"] as? Int {
        let stringVal = String(likes)
        cell?.numLikes.text = stringVal
        passLikes = likes
    }
    if let descrip = object?["Description"] as? String {
        passDescrip = descrip

    }
    let initialThumbnail = UIImage(named: "Unloaded")
    cell.customFlag.image = initialThumbnail
    if let thumbnail = object?["imageCover"] as? PFFile {
        cell.customFlag.file = thumbnail
        cell.customFlag.loadInBackground()


    }
    print("Finished loading!")


    return cell
}

//Original prepareForSegue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "toDetailScene") {

        // initialize new view controller and cast it as your view controller
        let viewController = segue.destinationViewController as! DetailViewController




        viewController.tripName = passName
        viewController.tripAuthor = passAuthor
        viewController.tripLikes = passLikes
        viewController.tripDescrip = passDescrip
    }
}

Later, the next view controller recieves it with this simple code: @IBOutlet weak var name: UILabel! @IBOutlet weak var likes: UILabel! @IBOutlet weak var author: UILabel! @IBOutlet weak var descrip: UITextView!

var currentObject : PFObject?

var tripName = ""
var tripAuthor = ""
var tripLikes = 0
var tripDescrip = ""

override func viewDidLoad() {
    super.viewDidLoad()



    //Hookup
    name.text = tripName
    likes.text = String(tripLikes)
    author.text = tripAuthor
    descrip.text = tripDescrip

}

I need to fix it so that it doesn't awkwardly show the wrong article/info all the time.

If I could simply pull the correct cell that they clicked on then I could pull the text from each label? So how could I go about getting the right cell?

BTW: It is segued to the next view controller just from the cell to the view controller with a show segue. (In a navigation controller)

Thanks so much!

1
If you are triggering the segue directly from the cell I think you will find that sender in prepareForSegue is the cell that was tapped - Paulw11
it is not really clear what is going on from the post or what you are asking...but when they tap a cell, cellForRowAtIndexPath will ensure that that tapped row will be processed - ksa_coder
also the segue better be from the view controller to the destination view controller and not from the cell. This may help - ksa_coder

1 Answers

0
votes

@Paulw11 answered this in a comment, and it solved my problem.

The sender: AnyObject? allowed me just to add:

let cell = sender as! CustomPFTableViewCell

and then from there all I had to do was update the method to hookup the new values of the text fields directly from the cell.

Here's that code:

let viewController = segue.destinationViewController as! DetailViewController         
let cell = sender as! CustomPFTableViewCell

        viewController.tripName = cell.nameTextLabel.text!
        viewController.tripAuthor = cell.authorTextLabel.text!
        viewController.tripLikes = Int(cell.numLikes.text!)!