json - 如何在 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;
};
};
}
解决方案
推荐阅读
- c - 如何将嵌套的 c struct 数据保存到磁盘?
- android - 无法使用 Kotlin、Retrofit、RxJava 和 MVVM 模式解析嵌套 JSON
- c - 比较 ptrdiff_t 和 size_t
- mysql - 无法用mysql创建表
- c++ - strlen() 不断将 p2 长度值添加到 p1 长度
- flutter - Flutter Web 图像选择器仅获取图像文件
- asp.net-core - 使用 oppeniddict 中的隐式和客户端凭据流访问令牌请求的授权失败
- python-3.x - 如何识别序列中缺失的日期并进行估算
- java - 关于java web层之间数据传输的问题
- lua - Lua-cURL: https get into string