如何向 UITableView 中的单元格添加复选标记使用自定义 UITableViewCell 子类和 dequeueReusableCell

问题描述

我想为我的表格视图单元格添加复选标记。我想只在我的 cell.accessoryType = UITableViewCellAccessorycheckmark 方法中实现 didSelectRowAt,但是在使用自定义 UITableViewCell 子类时我无法让它工作。

自定义单元格:

class TableCell: UITableViewCell {
    
    @IBOutlet weak var tableLabel: UILabel!
    
    var arrayOnetodisplay:ArrayOne?
    let arrayOne = ArrayOne()
    
    func displayTable(_ currentArrayOne:ArrayOne) {
        
        // Keep a reference to the cell
        arrayOnetodisplay = currentArrayOne
        
        // Get the Table Cells data
        var tableCell = arrayOne.tableCells
        
        // Set the instructionLabel
        tableLabel.text = currentArrayOne.tableCells![0]
        
    }

视图控制器:

extension ViewController: UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {

        // Make sure the array contains at least 1 item
        guard arrayOne.count > 0 else {
            return 0
        }
        
        // Return the number of items for this array
        let currentArrayOne = arrayOne[currentArrayOneIndex!]
        
        if currentArrayOne.tableCells != nil {
            return currentArrayOne.tableCells!.count
        }
        else {
            return 0
        }

    }

    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        // Get a cell
        let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell",for: indexPath) as! TableCell
        
        // Get the instruction that the tableView is asking about
        let tableLabel = cell.tableLabel
        
        if tableLabel != nil {
            
            let currentArrayOne = arrayOne[currentArrayOneIndex!]
            
            if currentArrayOne.tableCells != nil && indexPath.row < currentArrayOne.tableCells!.count {
                // Set the text for the label
                tableLabel!.text = currentArrayOne.tableCells![indexPath.row]
            }
        }
        
        // Return the cell
        return cell

    }
    
    func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
        

    }

}

当我尝试在 ViewController 中实现 cell.accessoryType 时,Xcode 无法识别 accessoryType。我相信这是因为我的方法是针对 UITableView 而不是 UITableViewCell,所以使用自定义单元格的额外问题给我造成了一些困惑。非常感谢任何指导!

解决方法

抱歉,您的方法完全错误。切勿将视图用作数据源类型。不要。

创建一个模型,这是一个带有 nameisSelected 属性的简单示例

struct Model {
    let name : String
    var isSelected = false
}

并在控制器ViewController声明一个包含Model个实例的数据源数组

var arrayOne = [Model(name: "Foo"),Model(name: "Bar"),Model(name: "Baz")]

numberOfRows 中的扩展中,只返回数组中的项目数(您的 guard 表达式根本没有意义),在 cellForRow 中根据 {{1} 设置复选标记}} 并在 isSelected 中切换 didSelect 属性并重新加载该行。

isSelected

在自定义单元格中只声明出口,没有别的

extension ViewController: UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return arrayOne.count
    }

    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        // Get a cell
        let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell",for: indexPath) as! TableCell
        
        // Get the item of the data source array
        let item = arrayOne[indexPath.row]
        
        // update the view
        cell.tableLabel = item.name
        cell.accessoryType = item.isSelected ? .checkmark : .none   
        
        // Return the cell
        return cell
    }
    
    func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
        // toggle isSelected of the row in the model
        arrayOne[indexPath.row].isSelected.toggle()
      
        // reload the row to update the view
        tableView.reloadRows(at: [indexPath],with: .none)   
    }

}

这种方法的好处是模型视图总是同步的。

,

不确定你想做什么。表格视图的常用方法是不将表格视图单元保存在数组中。模型-视图-控制器 (MVC) 方式是将单元格的内容(文本、图像、checkedOrNotChecked)保存在视图控制器内部的结构体或对象数组中(该数组就是模型)。然后在 CellForRow 中,使用 array[indexPath.row] 元素设置单元格的内容,如果 checkedOrNotCheckindicator 将 cell.accessoryType 设置为复选标记(单元格是视图)。 在 didSelectRow 中,您只需设置 indexPath.row 元素的 checkOrNotCheck 指示器,然后重新加载表格视图或仅在 indexPath 处重新加载单元格。 ViewController 是 MVC 中的控制器

,

您应该在 accessoryType 方法中设置您的 cellForRowAt,尝试将其重写为:

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

        cell.accessoryType = .checkmark
        
        guard let arrayOneIndex = currentArrayOneIndex,arrayOneIndex < arrayOne.count else { return cell }

        let currentArrayOne = arrayOne[arrayOneIndex]

        guard let tableCells = currentArrayOne.tableCells,indexPath.row < tableCells.count else { return cell }

        cell.tableLabel.text = tableCells[indexPath.row]
        
        return cell
    }