简体   繁体   中英

Change button title for selected row in TableView Swift

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)
    }
    
}

You can do it in this delegate method as below:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
for (id object in cell.superview.subviews) {
    if ([object isKindOfClass:[UITableViewCell class]]) {
        UITableViewCell *cellNotSelected = (UITableViewCell*)object;
        cellNotSelected.textLabel.textColor = [UIColor blackColor];
    }
}

cell.textLabel.textColor = [UIColor redColor];

[tableView deselectRowAtIndexPath:indexPath animated:NO];

}

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] Or AnyHashable 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 command

Reference Code

import UIKit

class FirstViewController: UIViewController {
  
  @IBOutlet weak var tableView: UITableView!
  var commend: [IndexPath: String] = [:]
  
}

extension FirstViewController: UITableViewDelegate, UITableViewDataSource{
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ListTableViewCell.self), for: indexPath) as! ListTableViewCell
    cell.button.setTitle(commend[indexPath] ?? "Add commend", for: .normal)
    cell.label.text = "\(indexPath.row)"
    cell.button.addTarget(self, action: #selector(self.addCommentsAction(_:)), for: .touchUpInside)
    return cell
  }
  
  @objc func addCommentsAction(_ sender: UIButton) {
    guard let nextVC = UIStoryboard(name: "Main", bundle: .some(.main)).instantiateViewController(withIdentifier: String(describing: CommendViewController.self)) as? CommendViewController else { return }
    let buttonPosition:CGPoint = sender.convert(CGPoint.zero, to: self.tableView)
    guard let indexPath = self.tableView.indexPathForRow(at: buttonPosition) else{
      return 
    }
    nextVC.delegate = self
    nextVC.indexPath = indexPath
    self.navigationController?.pushViewController(nextVC, animated: true)
  }
  
}

extension FirstViewController: DataPassingDelegate{
  func commendForIndex(_ string: String, indexPath: IndexPath) {
    self.commend[indexPath] = string
    self.tableView.reloadData()
  }
}

class ListTableViewCell: UITableViewCell{
  @IBOutlet weak var label: UILabel!
  @IBOutlet weak var button: UIButton!
}


protocol DataPassingDelegate: AnyObject {
  func commendForIndex(_ string: String, indexPath: IndexPath)
}

class CommendViewController: UIViewController {

  @IBOutlet weak var textView: UITextView!

  weak var delegate: DataPassingDelegate?
  var indexPath = IndexPath()
  
  @IBAction func doneCommend(_ sender: UIButton){
    self.delegate?.commendForIndex(textView.text, indexPath: indexPath)
  }
    
}

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