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

标签 ios arrays uitableview

在 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/

相关文章:

ios - 未能安装或启动 jenkins 中的测试运行程序错误

android - Flutter 如何在没有任何用户交互的情况下从一个页面导航到另一个页面

javascript - 带有键值的 ng-if 条件 - Angular js

ios - 标签为空时的 UITableView 单元格垂直居中

ios - 当我按下按钮时,我想要添加文本字段的内容 - IOS TableView

ios - 点击表格 View 中的按钮 - 错误的单元格

ios - Flutter 2.8.0 不会在 iOS 模拟器上部署和运行项目(在 2.5.3 上运行)

ios - 旋转 90 度更改图像大小和裁剪 - Swift 3

javascript - 数组转换

javascript - 如何使用 VS Code 在 JavaScript 中将 csv 文件的内容存储在数组中