簡體   English   中英

在特定行的表格視圖部分更改按鈕標題

[英]Change button title in section of tableview on specific row

我有查看 controller ,其中有多個 tableview 部分。 在第 0 節中,我有多行。 當我單擊按鈕時,每一行都有名為“添加評論”的按鈕,它會將我推到其他視圖 controller 當我寫一些東西並按下完成按鈕然后通過委托傳遞文本字段數據並將其設置在按鈕標題中時具有文本字段。 但問題是我的按鈕出現在所有行更改值中。 我只希望部分中的選定行更改其按鈕標題。 下面是我的第一個視圖控制器的代碼

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 {
        if(indexPath.section == 0)
        {
            let indetifier =  "MyTabTableViewCell"
            let cell = tableView.dequeueReusableCell(withIdentifier: indetifier, for: indexPath) as! MyTabTableViewCell
cell.addCommentsButton.setTitle(addCommentsValueStore, for: UIControl.State.normal)
}
return cell
        }

extension MyTabViewController: AddCommentsDelegate{
    func passAddComments(instruction: String) {
        addCommentsValueStore = instruction
        print(addCommentsValueStore)
    }
}
}

下面是第二個視圖 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) {

        self.navigationController?.popViewController(animated: true)
    }
    
    @IBAction func DoneActionClick(_ sender: Any) {
        let dataToBeSent = addCommentsTextField.text
        self.passAddCommentsDelegate?.passAddComments(instruction: dataToBeSent!)
        self.navigationController?.popViewController(animated: true)
    }
    
}

通過在單元格按鈕上使用tag ,您可以解決問題。

在您的cellForRowAt委托方法上,放置以下行:

cell.addCommentsButton.tag = indexPath.item

現在您確切知道 select 是哪個按鈕完成的,那么您可以使用此標簽來指定 tableView 中的哪一行應該更改其標題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM