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

标签 ios swift uitableview

我在通过用户点击自定义单元格中的按钮从自定义单元格传递数据时遇到问题。由于单元格被重复使用,我有时会得到错误的单元格数据。我想知道是否有一种完整的证明方法可以始终将正确的单元格数据发送到每个单元格中的按钮,无论屏幕上当前是哪个单元格。下面是我的代码。非常感谢任何帮助。

我的自定义单元格:

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

关于ios - 从重复使用的自定义单元格中的按钮传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39306613/

相关文章:

ios - 上下 PageViewController

swift - 选择 TableView 行时出现奇怪的显示行为

ios - 对于特定对象,使一个数组等于另一个数组

ios - 如何删除手指点击时的 UITableViewCell 操作?

ios - 解析 shoutcast .pls 流以在 iOS 中显示元数据

ios - 为 UITableView 中的每个部分使用不同的自定义单元格

swift - NSPersistentDocument、Swift、macOS 和 Storyboard — 如何获取 ManagedObjectContext?

swift - 将 NumberFormatter.Style.spellOut 转换为 Int

iOS 今日扩展 UITableView

ios - 什么应该拥有 MVC 模式中的模型?