首页 > 解决方案 > 如何为 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) {
        

    }

}

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

标签: iosswiftuitableviewcustom-cellaccessorytype

解决方案


对不起,你的方法完全错误。永远不要将视图用​​作数据源类型。不。

创建一个模型,这是一个带有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设置复选标记isSelecteddidSelect切换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)   
    }

}

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

class TableCell: UITableViewCell {
    
    @IBOutlet weak var tableLabel: UILabel!
        
}

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


推荐阅读