ios - 如何为选定的 tableView 行创建自定义图像复选标记附件?
问题描述
如何分配自定义图像以及选择和取消选择.checkmark
tableView 附件?
是否也可以将此自定义附件放置在 tableView 行的左侧并在 tableView 中始终可见?
当 tableView 首次加载时,附件仍然显示为取消选择,基本上类似于 Apple 提醒应用程序。
这是我正在寻找的示例:
取消选择:
已选择:
目前,这就是我所拥有的:
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
}
解决方案
这是一个示例代码。您需要为我的案例创建自己的附件视图我刚刚在自定义视图中添加了一个圆圈。之后您只需将隐藏设置为 true 或 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")
}
}
推荐阅读
- python - 如何在出现此错误的 vscode 中下载 python linter
- formatting - Qnx-neutrino 无法使用 fdisc 格式化我的分区,进程冻结
- r - 更改 R 图中图例的颜色
- android - Android 应用内购买:BillingClient:getPurchase() 失败。响应码:3
- jquery - 用于多项目显示的 Bootstrap 4 轮播包装问题
- php - 三叉树的深度 - 传销级别
- asp.net - 添加富文本编辑器以在 Asp.net 核心 MVC 内部添加和显示富 html
- javascript - 为什么 originalPositionFor() 为 Mozilla 的源地图库中的有效位置返回 null
- python - 使用python检索多个
xml中的文本 - python - 关于 64 位有符号整数的最大可计算值的混淆