gpt4 book ai didi

swift - 在 UITableView 的单元格中获取文本和按钮 - Swift 3

转载 作者:行者123 更新时间:2023-11-28 10:14:20 25 4
gpt4 key购买 nike

我正在尝试在每个单元格中实现一个按钮,该按钮是由在第二个 View Controller 上使用的创建的(参见图像)。这个按钮应该做一些代码,但我似乎无法弄清楚如何让按钮也在那里。真的不能用按钮制作一组新的元素,对吗?我试过在 SO 上搜索,但大部分代码都是“旧的”,并且有很多我不理解的代码仍然需要更改。

enter image description here

在 FirstViewController 上是:

import UIKit

var list = ["Buy milk", "mow the lawn", "Run"]

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var myTableView: UITableView!

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (list.count)
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]

return(cell)
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
list.remove(at: indexPath.row)
myTableView.reloadData()
}
}

override func viewDidAppear(_ animated: Bool) {
myTableView.reloadData()
}

在 SecondViewContoller 上它是:

class SecondViewController: UIViewController {

@IBOutlet weak var input: UITextField!

@IBAction func addItem(_ sender: Any) {
if ( input.text != "") {
list.append(input.text!)
input.text = ""
}
}

代码来自 TheSwiftGuy,我不拥有任何代码,只是想尝试向其中添加我自己的功能! ^^

最佳答案

为什么你需要在每个单元格上都有一个按钮你可以实现默认即 TableView 方法。

didSelectRowAtIndexPath

有了这个你就可以解决你的问题了

关于swift - 在 UITableView 的单元格中获取文本和按钮 - Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43321688/

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