gpt4 book ai didi

ios - 使用表格单元格内的按钮从原型(prototype)单元格传递数据

转载 作者:行者123 更新时间:2023-12-01 18:04:26 25 4
gpt4 key购买 nike

我有动态填充原型(prototype)单元格的数据。
它只会根据需要创建尽可能多的数据单元。
我遇到的问题是单击 UIButton 时将数据传递到详细 View 。

在每个动态单元格内都有一个 UIButton,按下该按钮时会将单元格内的任何数据传递到详细 View 中。

我将数据加载到表格 View 中没有问题,我根本无法正确传递值。

这是我在行的单元格:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "part", for: indexPath) as! partsTableCell

let part = partList[0]

cell.a.text = String(part.a)
cell.b.text = part.b
cell.c.text = part.c
cell.d.text = part.d
cell.e.text = part.e
cell.f.text = part.f

self.tableView.rowHeight = 250
cell.layer.borderColor = UIColor.darkGray.cgColor
cell.layer.borderWidth = 1
cell.layer.cornerRadius = 5
cell.clipsToBounds = true
//tried using tags, but no luck
//cell.myButton.tag = indexPath.row

return cell
}

为了彻底,我为表格单元格设置了一个自定义类。
class partsTableCell: UITableViewCell {
@IBOutlet weak var a: UILabel!
@IBOutlet weak var b: UILabel!
@IBOutlet weak var c: UILabel!
@IBOutlet weak var d: UILabel!
@IBOutlet weak var e: UILabel!
@IBOutlet weak var f: UILabel!
@IBOutlet weak var myButton: UIButton!
}

这是我传递数据的地方:
    func prepare(for segue: UIStoryboardSegue, sender: UIButton) {


if (segue.identifier == "partDetail") {
let detailVC = segue.destination as? PartsVCDetail
let indexPath = self.tableView.indexPathForSelectedRow
let part = dataList[indexPath.row]
detailVC?.a = part.a!
detailVC?.b = part.b!
detailVC?.c = part.c!
detailVC?.d = part.d!
detailVC?.e = part.e!
detailVC?.f = part.f

}
}

一切正常,但是当我单击按钮并执行 segue 时。数据在另一侧显示为空白或零。

我错过了一些明显的东西吗?

任何帮助都会很棒!

谢谢

编辑 -

这就是我设置详细 VC 的方式
@IBOutlet weak var qq: UILabel!
@IBOutlet weak var ww: UILabel!
@IBOutlet weak var ee: UILabel!
@IBOutlet weak var rr: UILabel!
@IBOutlet weak var tt: UILabel!
@IBOutlet weak var yy: UILabel!
@IBOutlet weak var uu: UILabel!

var a: Int64!
var b: String = ""
var c: String = ""
var d: String = ""
var e: String = ""
var f: String = ""
var g: String = ""


override func viewDidLoad() {
super.viewDidLoad()
//lineNumber.text = "test"

print(a,"************")
print(b, "************")
print(c, "************")
print(d, "************")
print(e, "************")
print(f,"************")
//ALL DATA ABOVE IS EITHER NULL OR EMPTY

qq.text = a as! String
ww.text = b
ee.text = c
rr.text = d
tt.text = e
yy.text = f
uu.text = g

}

最佳答案

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "part", for: indexPath) as! partsTableCell

let part = partList[0]

cell.a.text = String(part.a)
cell.b.text = part.b
cell.c.text = part.c
cell.d.text = part.d
cell.e.text = part.e
cell.f.text = part.f

self.tableView.rowHeight = 250
cell.layer.borderColor = UIColor.darkGray.cgColor
cell.layer.borderWidth = 1
cell.layer.cornerRadius = 5
cell.clipsToBounds = true

// give tag to your button and add target
cell.myButton.tag = indexPath.row
cell.myButton.addTarget(self, action: #selector(myButtonAction), for: .touchUpInside)

return cell

}
@objc func myButtonAction(sender:UIButton)  {
// in button action do your stuff like below

let part = partList[sender.tag]

let detailVC = UIStoryboard(name: "YourStoryBoard", bundle: nil).instantiateViewController(withIdentifier: "partDetail") as! PartsVCDetail
detailVC?.a = part.a!
detailVC?.b = part.b!
detailVC?.c = part.c!
detailVC?.d = part.d!
detailVC?.e = part.e!
detailVC?.f = part.f
self.navigationController?.pushViewController(detailVC, animated: true)

}

关于ios - 使用表格单元格内的按钮从原型(prototype)单元格传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54265368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com