复选框UIButton标题在点击时不会切换

时间:2017-05-01 22:36:20

标签: ios uibutton

此代码用于列出任务的tableViewController。当点击UIButton时,它应该将按钮的标题从空字符串切换到复选标记。出于某种原因,当我点击模拟器中的按钮时,没有任何反应,并且控制台中没有显示错误。任何人都知道为什么不切换?参考代码如下。任何帮助将不胜感激!谢谢大家!

这里是UITableViewController代码:

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(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)
    }

这里是UIButton的代码:

import UIKit

class CheckButton : UIButton {
    convenience init() {
        self.init(frame:CGRect.init(x: 0, y: 0, width: 20, height: 20))
        self.layer.borderWidth = 2
        self.layer.cornerRadius = 10
        self.titleLabel?.font = UIFont(name:"Georgia", size:10)
        self.setTitleColor(.black, for: .normal)
        self.check(false)
    }
    func check(_ yn:Bool) {
        self.setTitle(yn ? "✔" : "", for: .normal)
    }
    override init(frame:CGRect) {
        super.init(frame:frame)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

1 个答案:

答案 0 :(得分:1)

只有checkcb.check(rowChecked[indexPath.row])的所有来电rowChecked始终是[false, false, false, ...]的数组。

根据您在cb.check(tasks[indexPath.row].completed)中所做的事情,这应该是buttonTapped