简体   繁体   中英

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

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

Or use delegate to send that value to the VC the contains the tableView

First of all. You should have a model object which you are using it to load the values of 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. Then save, pass and use it where ever you want. Then implement a delegate method in your custom table cell class passing the indexPath. Then refresh the cell using tableView.reloadRows(at: [IndexPath(item:0,section:0)], with: .fade)

You can do it by closure or delegation

1: Closure

In your tableViewCell class create a variable like this

customObject is the object you passed the tableviewCell to load the data

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM