简体   繁体   English

如何为选定的tableView行创建自定义图像复选标记附件?

[英]How to create custom image check mark accessory for selected tableView rows?

How can I assign a custom image and for the select and deselect .checkmark tableView accessory? 如何分配自定义图像以及选择和取消选择.checkmark tableView附件?

Is it possible to also position this custom accessory on the left side of the tableView row and have constantly visible in the tableView? 是否可以将此自定义附件放置在tableView行的左侧并在tableView中始终可见?

When the tableView first loads the accessory is still shown just deselected essentially similar to the Apple Reminders app. 当tableView首次加载时,仍会显示刚刚取消选择的附件,基本上类似于Apple Reminders应用程序。

Here is an example of what I am looking for: 这是我正在寻找的一个例子:

Deselected: 取消:

在此输入图像描述

Selected: 选择:

在此输入图像描述

Currently, this is what I have: 目前,这就是我所拥有的:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    tableView.cellForRow(at: indexPath)?.accessoryType = .none
}

Here is a sample code. 这是一个示例代码。 You need to create your own accessory view for my case I Just added one circle inside a custom view.After that you just need to make hidden true or false. 您需要为我的案例创建自己的附件视图我只是在自定义视图中添加了一个圆圈。之后您只需要隐藏真或假。

extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.accessoryView = CheckMarkView.init()
    cell.accessoryView?.isHidden = true
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
    tableView.cellForRow(at: indexPath)?.accessoryView?.isHidden = false
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    tableView.cellForRow(at: indexPath)?.accessoryView?.isHidden = true
}

}

class CheckMarkView: UIView {
override init(frame: CGRect) {
    super.init(frame: frame) // calls designated initializer
    let img = UIImage(named: "circle.png") //replace with your image name
    let imageView: UIImageView = UIImageView(image: img)
    self.addSubview(imageView)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM