ios - 复选标记不会打开/关闭

标签 ios swift uitableview

在我的表格 View 单元格中有一个复选标记,它不会打开或关闭。不知道为什么。无论我按复选标记多少次来选中/取消选中它,它都不会改变状态。

ChecklistViewController.swift

import UIKit

class ChecklistViewController: UITableViewController {

    var items: [ChecklistItem]

    required init?(coder aDecoder: NSCoder) {
        items = [ChecklistItem]()

        let row0item = ChecklistItem()
        row0item.text = "Walk the dog"
        row0item.checked = false
        items.append(row0item)

        let row1item = ChecklistItem()
        row1item.text = "Brush my teeth"
        row1item.checked = true
        items.append(row1item)

        let row2item = ChecklistItem()
        row2item.text = "Learn iOS development"
        row2item.checked = true
        items.append(row2item)

        let row3item = ChecklistItem()
        row3item.text = "Soccer practice"
        row3item.checked = false
        items.append(row3item)

        let row4item = ChecklistItem()
        row4item.text = "Eat ice cream"
        row4item.checked = true
        items.append(row4item)

        super.init(coder: aDecoder)
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("ChecklistItem", forIndexPath: indexPath)
        let item = items[indexPath.row]
        let label = cell.viewWithTag(1000) as! UILabel
        label.text = item.text

        configureTextForCell(cell, withChecklistItem: item)
        configureCheckmarkForCell(cell, withChecklistItem: item)

        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        if let cell = tableView.cellForRowAtIndexPath(indexPath) {
            let item = items[indexPath.row]
            item.checked = !item.checked

            item.toggleChecked()
            configureCheckmarkForCell(cell, withChecklistItem: item)
        }

        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }

    func configureCheckmarkForCell(cell: UITableViewCell, withChecklistItem item: ChecklistItem) {


        if item.checked {
            cell.accessoryType = .Checkmark
        } else {
            cell.accessoryType = .None
        }


    }

    func configureTextForCell(cell: UITableViewCell, withChecklistItem item: ChecklistItem) {
        let label = cell.viewWithTag(1000) as! UILabel
        label.text = item.text
    }

    @IBAction func addItem() {
        let newRowIndex = items.count

        let item = ChecklistItem()
        item.text = "I am a new row"
        item.checked = false
        items.append(item)

        let indexPath = NSIndexPath(forRow: newRowIndex, inSection: 0)
        let indexPaths = [indexPath]
        tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)
    }


}

ChecklistItem.swift

导入基金会

class ChecklistItem {
    var text = ""
    var checked = false

    func toggleChecked() {
        checked = !checked
    }
}

最佳答案

我认为你在 tableView didSelectRowAtIndexPath 中有错字

//1
item.checked = !item.checked //2
item.toggledChecked() //3
//4
  1. //假设 item.checked == true
  2. item.checked = !item.checked(false)//假
  3. toggleChecked -> item.checked = !item.checked//true
  4. //item.checked == item.checked为1,没有变化

所以您可能需要删除第 2 行。

关于ios - 复选标记不会打开/关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37076704/

相关文章:

ios - SWIFT 中类似于 UIColor.systemgray 的任何标准字符串

ios - Swift:将容器 View 中的几个标签设置为容器的比率/百分比

arrays - Xcode Swift 检查数组是否包含对象

swift - 如何为 NSStatusBarButton 注册点击 Action ?

iphone - 在 iPhone 上检测长按

ios - 为同一应用程序中的不同屏幕制作可重用表格 View 的最佳方法是什么?

ios - 解散弹出窗口后的第一响应者/焦点

ios - 单击TableViewCell时如何将数据从NSObject传递到另一个ViewController?

iOS UITableView 无法为每个单元格计算正确的单元格高度

javascript - 使用 contains 查找 tr td jquery 中的特定文本