gpt4 book ai didi

ios - 类型 '_' 的值没有成员

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

在 cellForRowAt 下的 cb.check(self.rowChecked[indexPath.row]) 行中,即使我将 rowChecked 设置为数组,我也会收到“'LolFirstTableViewController' 类型的值没有成员'rowChecked'”带有 tasks.count 项目数的 bool 值。我是否需要在 cellForRowAt 之外的其他地方初始化 rowChecked 或者我在这里做错了什么?这段代码的目的是让一个复选框显示在表格的每个单元格中,您可以在其中单击它以将附件更改为复选标记,然后再次单击它以取消选中它。复选框本身是一个单独的自定义类,称为 CheckButton。我还在学习 Swift,所以非常感谢任何帮助!谢谢!

import UIKit

class LoLFirstTableViewController: UITableViewController {

var tasks:[Task] = taskData

override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 60.0
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

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

@IBAction func cancelToLoLFirstTableViewController(_ segue:UIStoryboardSegue) {
}

@IBAction func saveAddTask(_ segue:UIStoryboardSegue) {
if let AddTaskTableViewController = segue.source as? AddTaskTableViewController {

if let task = AddTaskTableViewController.task {
tasks.append(task)

let indexPath = IndexPath(row: tasks.count-1, section: 0)
tableView.insertRows(at: [indexPath], with: .automatic)
}
}
}

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

let task = tasks[indexPath.row]
cell.task = task

var rowChecked: [Bool] = Array(repeating: false, count: tasks.count)

if cell.accessoryView == nil {
let cb = CheckButton()
cb.addTarget(self, action: #selector(buttonTapped(_:forEvent:)), for: .touchUpInside)
cell.accessoryView = cb
}
let cb = cell.accessoryView as! CheckButton
cb.check(self.rowChecked[indexPath.row])

return cell
}

func buttonTapped(_ target:UIButton, forEvent event: UIEvent) {
guard let touch = event.allTouches?.first else { return }
let point = touch.location(in: self.tableView)
let indexPath = self.tableView.indexPathForRow(at: point)
var tappedItem = tasks[indexPath!.row] as Task
tappedItem.completed = !tappedItem.completed
tasks[indexPath!.row] = tappedItem
tableView.reloadRows(at: [indexPath!], with: UITableViewRowAnimation.none)
}

最佳答案

您将 rowChecked 声明为局部变量并使用 self.rowChecked 调用它,就像它是类属性一样。

要解决此问题,请删除 rowChecked 之前的 self.

旧:

cb.check(self.rowChecked[indexPath.row])

新:

cb.check(rowChecked[indexPath.row])

可能还有其他问题,但这是您的代码当前出现错误的原因。

关于ios - 类型 '_' 的值没有成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43712911/

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