i have a view controller in which there is a tableview cell having button on it. Button with title add comments. when i click on add comments button then it take me to the next page where textfield is present when i write something in it then press done button then my button title for all the cell changes. But i want only selected row button title should change. Below is my code of table view.
class MyTabViewController: UIViewController {
var addCommentsValueStore: String = "Add Comments"
@IBOutlet weak var tabTableView : ContentWrappingTableView!
@IBAction func addCommentsAction(_ sender: UIButton) {
guard let nextVC = MyCommentsRouter.getMyCommentsViewScreen() else { return }
nextVC.passAddCommentsDelegate = self
self.navigationController?.pushViewController(nextVC, animated: true)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let indetifier = "MyTabTableViewCell"
let cell = tableView.dequeueReusableCell(withIdentifier: indetifier, for: indexPath) as! MyTabTableViewCell
cell.addCommentsButton.setTitle(addCommentsValueStore, for: UIControl.State.normal)
}
}
extension MyTabViewController: AddCommentsDelegate{
func passAddComments(instruction: String) {
addCommentsValueStore = instruction
print(addCommentsValueStore)
}
}
below is the code of next view controller:
import UIKit
protocol AddCommentsDelegate{
func passAddComments(instruction: String)
}
class MyCommentsViewController: UIViewController {
@IBOutlet var addCommentsTextField: UITextField!
var passAddCommentsDelegate: AddCommentsDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func backActionClick(_ sender: UIButton) {
// guard let nextVC = MyTabRouter.getMyTabViewScreen() else { return }
self.navigationController?.popViewController(animated: true)
}
@IBAction func DoneActionClick(_ sender: Any) {
let dataToBeSent = addCommentsTextField.text
self.passAddCommentsDelegate?.passAddComments(instruction: dataToBeSent!)
self.navigationController?.popViewController(animated: true)
}
}
2
Answers
You can do it in this delegate method as below:
}
you need to change you logic for done those things. First you need to declare string Wrong. Because string only hold a single value. You need to declare variable as
Dictionary
[IndexPath:String]
OrAnyHashable Dictionary
for store the value. I will give reference code below feel free to refer and if any doubt in the code please ask me in commandReference Code