ios - 想要配件复选标记仅在点击右侧时显示

标签 ios uitableview

我有一个带有单元格的 TableView,当按下单元格中的任意位置时,它会在右侧添加一个复选标记。如果在右侧点击单元格,我只希望显示复选标记。这是来自 TableViewController 的相关代码部分:

    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


        if task.completed {
            cell.accessoryType = UITableViewCellAccessoryType.checkmark;
        } else {
            cell.accessoryType = UITableViewCellAccessoryType.none;
        }

        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)

        var tappedItem = tasks[indexPath.row] as Task
        tappedItem.completed = !tappedItem.completed
        tasks[indexPath.row] = tappedItem

        tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.none)
    }

}

有没有一种简单的方法可以做到这一点,或者使用 Storyboard来做到这一点?我的 Swift 技能还有很多不足之处。任何帮助,将不胜感激!谢谢!

最佳答案

为什么不提供一个用户可以点击并显示复选标记的实际按钮作为附件 View ,而不是内置的复选标记附件类型?例如,该按钮可能在正常情况下显示为一个空心圆圈,而在用户点击它时显示为一个带有复选标记的圆圈。

否则,您希望用户猜测一个晦涩的界面,而通过这种方式,您点击此处将任务标记为已完成是非常明显的。

例子:

enter image description here

为此,我创建了一个按钮子类并将每个单元格的 accessoryView 设置为它的一个实例:

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

按钮的标题可以是空字符串或复选标记字符,从而提供您点击按钮时看到的效果。此代码来自 cellForRowAt::

    if cell.accessoryView == nil {
        let cb = CheckButton()
        cb.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        cell.accessoryView = cb
    }
    let cb = cell.accessoryView as! CheckButton
    cb.check(self.rowChecked[indexPath.row])

(其中 rowChecked 是 Bool 数组)。

关于ios - 想要配件复选标记仅在点击右侧时显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43035765/

相关文章:

iOS UIRefreshControl 在单 View 应用程序中,没有表

ios - 有没有办法在 iOS 中使用表格 View 设计类似 Tinder/Twitter-self-profile 的标题效果?

objective-c - View 的变换不会随着设备旋转而改变

ios - 伐木 worker iOS : How to write encrypted logs (Block Encryption)

ios - GSSendEvent - 注入(inject)触摸事件 iOS

ios - 是否可以使用 SKScene 数组实现 iCarousel?

ios - 如何在 iOS 中从 Nsarray 中搜索?

iphone - 通过导航栏按钮的操作在 UITableview 中设置删除按钮

ios - UITableViewController 中的 UITableView 行分隔符关闭了吗?

IOS UiTableView Cells 我怎样才能让它的高度可调