In the cb.check(self.rowChecked[indexPath.row]) line under cellForRowAt I'm getting a "Value of type 'LolFirstTableViewController' has no member 'rowChecked'" even though I set up rowChecked to be an array of Booleans with tasks.count number of items. Do I need to initialize rowChecked somewhere else besides cellForRowAt or what am I doing wrong here? The point of this code is to make a checkbox show up in each cell of a table where you can click it to change the accessory to a check mark, and click it again to uncheck it. The check box itself is a separate custom class called CheckButton. I'm still learning Swift so any help would be greatly appreciated! Thank you!
在cellForRowAt下的cb.check(self.rowChecked [indexPath.row])行中,我得到一个“类型值为'LolFirstTableViewController'没有成员'rowChecked'”,即使我将rowChecked设置为一个布尔数组tasks.count项目数。我是否需要在除了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)
}
2 个解决方案
#1
1
You are declaring rowChecked
as a local variable and calling it with self.rowChecked
as if it were a class property.
您将rowChecked声明为局部变量并使用self.rowChecked调用它,就好像它是一个类属性一样。
To solve this issue, remove the self.
before rowChecked
.
要解决此问题,请删除self。在rowChecked之前。
Old:
cb.check(self.rowChecked[indexPath.row])
New:
cb.check(rowChecked[indexPath.row])
There might be further issues, but that's the reason for the error as your code currently stands.
可能还有其他问题,但这就是您的代码目前存在错误的原因。
#2
1
You have the line: var rowChecked: [Bool] = Array(repeating: false, count: tasks.count)
inside the tableView:cellForRowAt
method, so it's a local variable, it's not a property of the LolFirstTableViewController
class.
你有一行:var rowChecked:[Bool] = tableView:cellForRowAt方法中的数组(重复:false,count:tasks.count),所以它是一个局部变量,它不是LolFirstTableViewController类的属性。
That means you need to change this line: cb.check(self.rowChecked[indexPath.row])
to cb.check(rowChecked[indexPath.row])
(Removed self.
).
这意味着你需要更改这一行:cb.check(self.rowChecked [indexPath.row])到cb.check(rowChecked [indexPath.row])(删除自己。)。
#1
1
You are declaring rowChecked
as a local variable and calling it with self.rowChecked
as if it were a class property.
您将rowChecked声明为局部变量并使用self.rowChecked调用它,就好像它是一个类属性一样。
To solve this issue, remove the self.
before rowChecked
.
要解决此问题,请删除self。在rowChecked之前。
Old:
cb.check(self.rowChecked[indexPath.row])
New:
cb.check(rowChecked[indexPath.row])
There might be further issues, but that's the reason for the error as your code currently stands.
可能还有其他问题,但这就是您的代码目前存在错误的原因。
#2
1
You have the line: var rowChecked: [Bool] = Array(repeating: false, count: tasks.count)
inside the tableView:cellForRowAt
method, so it's a local variable, it's not a property of the LolFirstTableViewController
class.
你有一行:var rowChecked:[Bool] = tableView:cellForRowAt方法中的数组(重复:false,count:tasks.count),所以它是一个局部变量,它不是LolFirstTableViewController类的属性。
That means you need to change this line: cb.check(self.rowChecked[indexPath.row])
to cb.check(rowChecked[indexPath.row])
(Removed self.
).
这意味着你需要更改这一行:cb.check(self.rowChecked [indexPath.row])到cb.check(rowChecked [indexPath.row])(删除自己。)。