[英]Pass data depends on the button in tableView cell
I have a TableView where I display all my data and each cell might have 1-2 buttons. 我有一个TableView,在其中显示所有数据,每个单元格可能有1-2个按钮。 I read many topics and understand how to add target for each button through my ViewController. 我阅读了许多主题,并了解了如何通过ViewController为每个按钮添加目标。 Since these buttons will be forwarded to the same VC and display images, I have the following code. 由于这些按钮将被转发到相同的VC并显示图像,因此我具有以下代码。 In my TableViewCell subclass I have 2 buttons 在我的TableViewCell子类中,我有2个按钮
class CODetailsTicketCell: UITableViewCel {
var onButtonTapped: (() -> Void)? = nil
@IBAction func firstBtnTapped(_ sender: UIButton) {
if let onButtonTapped = self.onButtonTapped {
onButtonTapped()
}
print("First button was pressed")
}
@IBAction func secondBtnTapped(_ sender: UIButton) {
if let onButtonTapped = self.onButtonTapped {
onButtonTapped()
}
print("Second button was pressed")
}
}
In my ViewController in cellForRowAt indexPath I have the following code 在cellForRowAt indexPath的ViewController中,我有以下代码
let message = messages[indexPath.row]
if let cell = tableView.dequeueReusableCell(withIdentifier: "COTicketsCell", for: indexPath) as? CODetailsTicketCell {
cell.configureCell(openTickets: message)
cell.onButtonTapped = {
self.performSegue(withIdentifier: "toImageVC", sender: message)
}
return cell
In order to pass the data through segue I use the following code in prepareForSegue 为了通过segue传递数据,我在prepareForSegue中使用以下代码
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toImageVC" {
let navigationController = segue.destination as? UINavigationController
if let targetController = navigationController?.topViewController as? ImageVC {
if let data = sender as? OpenTicketsData {
targetController.loadImageURL = URL(string: data.firstImageUrl)
}
}
}
}
Everything is working FINE but I can't check for button tag in prepareForSegue. 一切都很好,但我无法在prepareForSegue中检查按钮标签。 Basically, currently both buttons send the same data 基本上,当前两个按钮都发送相同的数据
targetController.loadImageURL = URL(string: data.firstImageUrl)
How can I pass data based on the button pressed? 如何根据按下的按钮传递数据? I tried to do something like this but seems it's wrong and not working. 我试图做这样的事情,但似乎是错误的,没有用。
let button = sender as? UIButton
if let data = sender as? OpenTicketsData {
if button?.tag == 1 {
targetController.loadImageURL = URL(string: data.firstImageUrl)
} else if button?.tag == 2 {
targetController.loadImageURL = URL(string: data.secondImageUrl)
}
}
You can either separate it into 2 different events or 您可以将其分为2个不同的事件,也可以
class CODetailsTicketCell: UITableViewCell {
var onButtonTapped: ((_ sender: UIButton) -> Void)? = nil
@IBAction func firstBtnTapped(_ sender: UIButton) {
if let onButtonTapped = self.onButtonTapped {
onButtonTapped?(sender)
}
print("First button was pressed")
}
@IBAction func secondBtnTapped(_ sender: UIButton) {
if let onButtonTapped = self.onButtonTapped {
onButtonTapped(sender)
}
print("Second button was pressed")
}
}
In your assignment of the onButtonTapped
, remember to add [weak self]
if you ever use self
to avoid the retain cycle. 在分配onButtonTapped
,如果您曾经使用self
来避免保留周期,请记住添加[weak self]
。
cell.onButtonTapped = { [weak self] sender in
if sender.tag == 1 {
// Do something
} else {
// Do other thing
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.