gpt4 book ai didi

swift - 如何在另一个 TableView 单元格内添加带有按钮的 TableView 单元格?

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

我正在尝试制作一个待办事项列表应用程序,但在尝试将子任务添加到我的主要待办任务中时遇到问题。我在每个 TableView 单元格内都有一个按钮,当按下此按钮时,我希望它添加另一个我可以键入并保存“子任务”的 TableView 单元格。

    //set up tableView row amount
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return taskData.count
}

//set up what is in the tableView row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//This is for a main task
let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! CustomTableViewCell

cell.label.text = taskData[indexPath.row]

return cell
}

最佳答案

使用委托(delegate)将数据从单元格传递到 ViewController,并重新加载 tableView。

自定义TableViewCell:

protocol CustomTableViewCellDelegate: class {
func customTableViewCellButtonClicked(_ cell: CustomTableViewCell)
}

class CustomTableViewCell: UITableViewCell {
weak var delegate: CustomTableViewCellDelegate?

func buttonClicked() {
self.delegate?.customTableViewCellButtonClicked(self)
}
}

View Controller :

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! CustomTableViewCell
cell.label.text = taskData[indexPath.row]
cell.delegate = self

return cell
}

func customTableViewCellButtonClicked(_ cell: CustomTableViewCell) {
// add the task you need from that cell to your tasks list.
taskData.append(....)
//reload your tableView
self.tableView.reloadData()
}

关于swift - 如何在另一个 TableView 单元格内添加带有按钮的 TableView 单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56780469/

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