简体   繁体   English

iOS-如何通过单击表格单元格中的按钮从表格单元格获取标签值?

[英]ios - How to get label value from table cell by click button in table cell?

I have an UITableView, in each cell it's have some label and a button. 我有一个UITableView,在每个单元格中都有一些标签和一个按钮。 I want to get all label value when I click the button. 我想在单击按钮时获取所有标签值。 How to do this? 这个怎么做? Thank you. 谢谢。

Create IBAction method for the button inside the cell custom class and inside it print 为单元格自定义类中的按钮创建IBAction方法,并在其中打印

   print("label text : \(self.lbl.text)")

Or use delegate to send that value to the VC the contains the tableView 或者使用委托将该值发送到包含tableView的VC

First of all. 首先。 You should have a model object which you are using it to load the values of label. 您应该有一个要用于加载label值的模型对象。 Use 采用

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {}

to get the index then try getting the value from the model using the index for example you have an array myArray then you could access the value using myArray[indexPath.row] to get the value. 要获取索引,然后尝试使用索引从模型中获取值,例如,您有一个数组myArray则可以使用myArray[indexPath.row]访问该值以获取该值。 Then save, pass and use it where ever you want. 然后保存,传递并在需要的地方使用它。 Then implement a delegate method in your custom table cell class passing the indexPath. 然后在您的自定义表格单元类中通过传递indexPath来实现一个委托方法。 Then refresh the cell using tableView.reloadRows(at: [IndexPath(item:0,section:0)], with: .fade) 然后使用tableView.reloadRows(at: [IndexPath(item:0,section:0)], with: .fade)刷新单元格。

You can do it by closure or delegation 您可以通过关闭或委托来完成

1: Closure 1:关闭

In your tableViewCell class create a variable like this 在您的tableViewCell类中,创建一个像这样的变量

customObject is the object you passed the tableviewCell to load the data customObject是您通过tableviewCell加载数据的对象

var cellData: customObject? {

    didSet {
        // do your loding labels in here
    }
}

var clickHandler: ((customObject) -> Void)!

and inside of you action button add this 然后在您的操作按钮内部添加此

 @IBAction func replyAction(_ sender: Any) {
    if let customObject = customObject {
        clickHandler(customObject)
    }
}

now go to where are you deque the table 现在去你在哪里摆桌子

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
   let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! YourCustomCell

   // add this
   cell.clickHandler = { customObject in 

   print("myCell.customObject = \(customObject)")

   }
}

this will do the magic 这会做魔术

2. Delegation 2.委托

Create a delegate methode like this 这样创建一个委托方法

protocol CustomCellDelegate {
    func getCustomObject(in cell: CustomCell, withCustomObject object: CustomObject)
}

now in your cell class add delegate variable 现在在您的单元格类中添加委托变量

 var delegate: CustomCellDelegate?

and inside of you action button add this 然后在您的操作按钮内部添加此

@IBAction func replyAction(_ sender: Any) {
    if let customObject = customObject {
        delegate.getCustomObject(in: self, withCustomObject: customObject)
    }
}

and now for the last part go to class you implemented the table view and this to where it shows 现在最后一部分上课了,您实现了表格视图,并将其显示到

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
   let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! YourCustomCell

   // add this
   cell.delegate = self
}

and inside of class you should add you delegate method 在类内部,您应该添加委托方法

extension YourClass: CustomCellDelegate {

    getCustomObject(in cell: CustomCell, withCustomObject object: CustomObject) {
         print("current cell data = \(CustomObject)"
    }

}

this will do the job too 这也可以做

Hop this will Helps 希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在表格视图单元格中单击按钮时获取标签值 - How to get label value in table view cell on button click in cell 如何从动态表格视图单元格 swift 获取按钮单击时的文本字段值 - How to get textfield value on button click from dynamic table view cell swift 如何在单元格的按钮单击中更改表格单元格中的UIImageView图像? - How to Change an UIImageView Image in a Table cell, on a button click from the cell? 在iOS表格单元格中单击按钮更改图像 - Change Image on click button in table cell in ios 如何从iPhone的“表格”视图中获取选定单元格的单元格值 - how to get the cell value of a selected cell from Table view in iPhone 表格单元格iOS野生动物园中的100%高度按钮/标签/输入 - 100% height button/label/input in a table cell iOS safari 如何在iOS中的表格视图单元格中添加按钮? - How to add a button to a table view cell in iOS? 从SQLite中检索列到表视图Swift iOS的单元格中的标签 - Retrieving a column from SQLite to a label in cell of table view Swift iOS 如何在表视图单元格中的按钮单击上获取标签以进行prepareForSegue - How to get label on button click in tableview cell for prepareForSegue 在iOS的表格视图中隐藏单元格的标签 - Hide a label of a cell in a table view in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM