خرید بک لینک

Vote count: 0

I'm having trouble passing data from a custom cell by a user tapping a button in that custom cell. I sometimes get the wrong cells data since the cell is being reused. I was wondering if there was a full proof way to always get the right cell data to its button in each cell no matter which cell is currently on the screen. Below is my code. Any help is greatly appreciated.

My Custom Cell:

protocol CustomCellDelegate {
  func segueWithCellData()
}

class CustomTableViewCell : UITableViewCell {
  var delegate = CustomCellDelegate?

  @IBAction func buttonTapped() {
    if let delegate = self.delegate {
        delegate.segueWithCellData()
     }
   }
}

MyTableViewController:

class MyTableViewController : UITableViewController, CustomCellDelegate {
   var posts = [Post]()
   var title: String!

   override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      let post = posts[indexPath.row]
      let cell =  tableView.dequeueReusableCellWithIdentifier("CustomCellReuseIdentifier", forIndexPath: indexPath)
      title = post.title

    cell.delegate = self        

    return cell
}

   func segueWithCellData() {
     self.performSegueWithIdentifier("passMyData", sender: self)
   }

   override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
     if segue.identifier == “passMyData” {
        let destination = segue.destinationViewController as! UINavigationController
        let targetVC = destination.topViewController as! nextVC
        targetVC.title = title    
      }

    }
 }

asked 43 secs ago

برچسب: pass data from view to controller,pass data from activity to fragment,pass data from one controller to another,pass data from php to javascript,pass data from controller to directive,pass data from view to controller mvc,pass data from html to javascript,pass data from controller to controller,pass data from fragment to fragment,pass data from one activity to another, نویسنده: استخدام کار تاريخ: شنبه 13 شهريور 1395 ساعت: 19:10

صفحه بندی