首页 > 解决方案 > 如何在 swift 中使用 JSON 响应更改单击的单元格按钮标签

问题描述

在我的表格视图单元格中,我添加了awerdedBtnLabel..awardedBtn如果我点击任何一个单元格上的按钮,那么我只需要更改点击的单元格标签文本

tableview 的代码:使用下面的代码,如果我单击任何一个单元格,awardedBtn那么所有单元格awerdedBtnLabel的文本都会发生变化,为什么?

如何仅在单击的单元格上更改标签文本,请指导我

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return viewproposalData?.result?.bids?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "ViewProposalTableVIewCell", for: indexPath) as! ViewProposalTableVIewCell


let bidData = viewproposalData?.result?.bids?[indexPath.row]
cell.propAmt.text = "$\(bidData?.amount ?? "")"

cell.awardedBtn.tag = indexPath.row
cell.awardedBtn.addTarget(self, action: #selector(connected(sender:)), for: .touchUpInside)

let postServData = viewproposalData?.result?.posted_services
if postServData?.is_awarded == "Y"{
    cell.awerdedBtnLabel.text = "Rewoke Award"
    cell.milestoneView.isHidden = false

}else{
    cell.awerdedBtnLabel.text = "Award"
    cell.milestoneView.isHidden = true
}

return cell
}

@objc func connected(sender: UIButton){


}

编辑:根据提到的答案,我已经尝试如下..但现在没有任何改变..我的意思不是所有单元格或不是一个..按钮标签文本保持不变..如果我点击任何按钮,则标签没有变化

class ViewProposalTableVIewCell: UITableViewCell, UICollectionViewDelegate,UICollectionViewDataSource {

 var onAwardBtn: ((_ isAwarded: String) -> Void)?
}

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return viewproposalData?.result?.bids?.count ?? 0//5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "ViewProposalTableVIewCell", for: indexPath) as! ViewProposalTableVIewCell

 let postServData = viewproposalData?.result?.posted_services

 let isAwarded = postServData?.is_awarded

    cell.onAwardBtn = { [weak self] (isAwarded) in

        if isAwarded == postServData?.is_awarded {
            
            cell.awerdedBtnLabel.text = "Rewoke Award"
            cell.milestoneView.isHidden = false
         
        }
        else{
            cell.awerdedBtnLabel.text = "Award"
            cell.milestoneView.isHidden = true
        }
    }
    

第二次编辑:

来自控制台的 JSON 响应:

JSON {
    jsonrpc = "2.0";
    result =     {
        bids =       (
                        {
                amount = 100;
                id = 153;

                "get_bid_user" =                 {
                    address = dfsdffafadf;
                };
               
            },
        );
        "posted_services" =         {
            "is_awarded" = Y;
        };
    };
    }

标签: jsonswiftbuttontableview

解决方案


推荐阅读