簡體   English   中英

如何為 UITableView 中的單元格添加復選標記(使用自定義 UITableViewCell 子類和 dequeueReusableCell)

[英]How to add a checkmark to cells in UITableView (using a custom UITableViewCell subclass and dequeueReusableCell)

我想為我的表格視圖單元格添加復選標記。 我想只在我的didSelectRowAt方法中實現cell.accessoryType = UITableViewCellAccessoryCheckmark ,但是在使用自定義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 ,所以使用自定義單元格的額外皺紋給我帶來了一些困惑。 非常感謝任何指導!

對不起,你的方法完全錯誤。 永遠不要將視圖用作數據源類型。 不。

創建一個model ,這是一個帶有nameisSelected屬性的簡單示例

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

並在controller ViewController中聲明一個包含Model實例的數據源數組

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

numberOfRows的擴展中,只返回數組中的項目數(您的guard表達式根本沒有意義),在cellForRow中根據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)   
    }

}

在自定義單元格中只聲明出口,沒有別的

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

這種方法的好處是model視圖總是同步的。

不知道你想做什么。 表格視圖的常用方法是不將 tableviewcell 保存在數組中。 model-view-controller(MVC)方式是將單元格的內容(文本,圖像,checkedOrNotChecked)保存在viewcontroller內部的struct數組或object中(數組即模型)。 然后在 CellForRow 中,使用 array[indexPath.row] 元素設置單元格的內容,如果選中OrNotCheckindicator,則將 cell.accessoryType 設置為復選標記(單元格是視圖)。 在 didSelectRow 中,您只需設置 indexPath.row 元素的 checkOrNotCheck 指示符,然后重新加載表格視圖或重新加載 indexPath 處的單元格。 ViewController 是 MVC 中的 controller

您應該在cellForRowAt方法中設置您的accessoryType類型,嘗試將其重寫為:

    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
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM