从重用的自定义单元格中的按钮传递数据

我无法通过用户点击该自定义单元格中的按钮从自定义单元格传递数据。 因为细胞被重复使用,我有时会得到错误的细胞数据。 我想知道是否有一种完全的certificate方式,无论在屏幕上当前哪个单元格,总是将正确的单元格数据输送到每个单元格中的按钮。 以下是我的代码。 任何帮助是极大的赞赏。

我的定制单元格:

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 } } } 

我的定制单元格:

 protocol CustomCellDelegate { func segueWithCellData(cell:CustomTableViewCell) } class CustomTableViewCell : UITableViewCell { var delegate = CustomCellDelegate? @IBAction func buttonTapped() { if let delegate = self.delegate { delegate.segueWithCellData(self) } } } 

CustomCellDelegate方法:

 func segueWithCellData(cell:CustomTableViewCell) { //Get indexpath of selected cell here let indexPath = self.tableView.indexPathForCell(cell) self.performSegueWithIdentifier("passMyData", sender: self) } 

因此,不需要标记细胞。 因为,您拥有所选单元格的indexPath,您可以从中获取数据并通过performSegueWithIdentifier方法的sender参数传递。

例如,

 func segueWithCellData(cell:CustomTableViewCell) { //Get index-path of selected cell here let selectedIndexPath = self.tableView.indexPathForCell(cell) let post = posts[selectedIndexPath.row] self.performSegueWithIdentifier("passMyData", sender: post) } 

并在prepareForSegue获取数据,如下所示:

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

我在几乎所有应用程序中都使用的完全certificate解决方案。 在UIButton的类别类中创建类型为NSIndexPath的自定义属性,并在cellForRowAtIndexPath函数中分配indexPath。 现在在按钮的回调中,通过数据源中的按钮indexPath.row找到索引处的对象。 这绝不会失败。

首先,您必须在MyTableViewController中创建这样的索引和标题字典:

 var titleDict = [Int:String]() 

在表格视图中将单元格的标记设置为索引,并在MyTableViewController中将标题附加到titleDict,如下所示:

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

并在我的自定义单元格中传递单元格委托方法中该单元格的标记值:

 protocol CustomCellDelegate { func segueWithCellData(index:Int) } class CustomTableViewCell : UITableViewCell { var delegate = CustomCellDelegate? @IBAction func buttonTapped() { if let delegate = self.delegate { let index = self.tag delegate.segueWithCellData(index) } } } 

并使用委托方法中的给定索引从titleDict访问标题,并在MyTableViewController中设置为title变量:

 func segueWithCellData(index:Int) { if let title = titleDict[index]{ self.title = title } self.performSegueWithIdentifier("passMyData", sender: self) } 

简单的解决方案:从数组(String)填充tableView并更新tableView。 如果要更改tableView中的某些数据,则需要更新数组并刷新tableView。

我在我的应用程序中使用此解决方案,效果很好。