gpt4 book ai didi

swift - 将选定的indexpath.row分配给另一个类中的变量

转载 作者:行者123 更新时间:2023-11-30 10:29:55 25 4
gpt4 key购买 nike

我在表格中显示了labelTEx中的数据。

当我单击labelTEx时,我打开另一个表类。

我想将UITableViewCell中点击的labelTExt的文本数据传输到DetayViewController中的formBaslik

也就是说,我想将我选择的gifsabaslik [indexPath.row]传输到DetayViewController中的detayBaslik。我怎样才能做到这一点?

DelayViewController

class DetayViewController: UIViewController {
var formBaslik = String()
}

TableViewController

class TableViewController: UIViewController{
var gifsayeniresim: [String] = []

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! TableViewCell
cell.labelTExt.text = self.gifsabaslik[indexPath.row]
cell.labelTExt.textColor = UIColor.white
cell.labelTExt.font = UIFont(name: "HelveticaNeue", size: 14.0)!
cell.labelTExt.backgroundColor = UIColor.lightGray
cell.labelLayout.constant = cell.labelTExt.contentSize.height
cell.labelTExt.isEditable = false

let tap = UITapGestureRecognizer(target: self,action: #selector(handleTaponTextField(_:)))
tap.numberOfTapsRequired = 1
tap.delegate = self
cell.labelTExt.isUserInteractionEnabled = true
cell.labelTExt.addGestureRecognizer(tap)


return cell
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "newo"{
if segue.destination is DetayViewController {

}
}

最佳答案

@SH 亚西姆 在 DetayViewController 中,声明

var formBaslik = String?

在TableViewController中,在prepare中写入

override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "newo" {
if let cell = sender as? UITableViewCell {
// You have clicked in a cell, but that may not have selected it: let's compute its indePath
let tapPosition:CGPoint = cell.convert(CGPoint.zero, to: self.table)
let indexPath = self.table.indexPathForRow(at: tapPosition)

if let vc = segue.destination as? DetayViewController {
vc.formBaslik = gifsabaslik[indexPath.row]
}
}
}

关于swift - 将选定的indexpath.row分配给另一个类中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59357546/

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