gpt4 book ai didi

ios - 试图将数组移动到表格 View

转载 作者:行者123 更新时间:2023-12-01 21:47:36 25 4
gpt4 key购买 nike

我正在尝试将目标从数组移动到 tableview 并且无法完成此任务。

这是我的代码。

import UIKit

class GoalsViewController: UIViewController {

@IBOutlet weak var goalTableView: UITableView!
@IBOutlet weak var goalCategoryLabel: UILabel!

var valueToPass = ""
var goals: [String] = []
let theEmptyModel: [String] = ["No data in this section."]

var firstGoals = ["run", "walk", "jog"]
var secondGoals = ["sleep", "rest"]

var goalCategoryText: String = ""

override func viewDidLoad() {
super.viewDidLoad()

goals = firstGoals
goalCategoryLabel?.text = goalCategoryText
goalTableView.delegate = self
goalTableView.dataSource = self
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "firstGoalsSegue" {
if goalCategoryText == "First Goals" {
let viewController = segue.destination as! GoalsViewController
viewController.firstGoals.append(valueToPass)
}
}
if segue.identifier == "secondGoalsSegue" {
if goalCategoryText == "Second Goals" {
let viewController = segue.destination as! GoalsViewController
viewController.secondGoals.append(valueToPass)
}
}

}

extension GoalsViewController: UITableViewDelegate, UITableViewDataSource {

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "GoalsCell_1", for: indexPath)
cell.textLabel?.text = goals[indexPath.row]
cell.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
cell.textLabel?.numberOfLines = 3
return cell
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "\(sectionHeader)"
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
valueToPass = goals[indexPath.row]
performSegue(withIdentifier: "firstGoalsSegue", sender: self)
performSegue(withIdentifier: "secondGoalsSegue", sender: self)
tableview.reloadData()
}
}

这是我的 Storyboard。
Storyboard

当前,选择一个按钮时,我被带到只显示空单元格的桌面屏幕。我将如何修复我的代码,以便在点击受尊重的按钮时将 firstGoals 和 secondGoals 的值传递给 tableview?

最佳答案

您需要设置 GoalTableViewdelegatedataSourceGoalsViewController , 像这样:

override func viewDidLoad() {
super.viewDidLoad()

goals = ["goal 1", "goal 2", "goal 3"]
goalCategoryLabel?.text = goalCategoryText
GoalTableView.delegate = self
GoalTableView.dataSource = self
GoalTableView.reloadData()
}

提示:尝试为实例保留以小写字母开头的名称,在这种情况下重命名 GoalTableViewgoalTableView .

关于ios - 试图将数组移动到表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62051983/

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