从重用的自定义单元格中的按钮传递数据 [英] Pass data from a button in reused custom cell
问题描述
我无法通过用户点击自定义单元格中的按钮从自定义单元格传递数据.由于单元格被重用,我有时会得到错误的单元格数据.我想知道是否有一种完整的证明方法可以始终将正确的单元格数据发送到每个单元格中的按钮,无论当前屏幕上的哪个单元格.下面是我的代码.非常感谢任何帮助.
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.
我的自定义单元格:
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 方法:
CustomCellDelegate Method:
func segueWithCellData(cell:CustomTableViewCell) {
//Get indexpath of selected cell here
let indexPath = self.tableView.indexPathForCell(cell)
self.performSegueWithIdentifier("passMyData", sender: self)
}
因此,不需要标记单元格.由于您拥有所选单元格的 indexPath,因此您可以从中获取数据并将其通过 performSegueWithIdentifier
方法的 sender
参数传递.
Hence, no need of tagging cell.
Since, you have indexPath of the selected cell, you can get data from this and pass this through sender
parameter of performSegueWithIdentifier
method.
例如
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
中的数据:
and, get the data inside prepareForSegue
as follows:
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
}
}
这篇关于从重用的自定义单元格中的按钮传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!