【问题标题】:Checkbox UIButton title not toggling when tapped点击时复选框 UIButton 标题不切换
【发布时间】:2017-05-01 22:36:20
【问题描述】:

此代码用于列出任务的 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")
    }
}

【问题讨论】:

  • 你试过调试吗?例如。在check 函数中设置断点或在此处添加日志。该方法甚至被调用了吗?如果是这样,检查(打印)出yn的值

标签: ios uibutton


【解决方案1】:

只有你每次调用checkcb.check(rowChecked[indexPath.row])rowChecked 总是[false, false, false, ...] 的数组。

根据您在buttonTapped 中的操作,这应该是cb.check(tasks[indexPath.row].completed)

【讨论】:

  • 啊,也许我应该为 indexPath 中每个项目的状态每次更新 rowChecked ?
  • 由于rowChecked只存在于函数中,可能没有理由保留它,因为tasks已经是真实的记录了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-20
  • 1970-01-01
  • 2016-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多