[英]Accessibility problem in reusable TableViewCell
The problem is that when cells are reusable, accesibilityLabel
doesn't work and VoiceOver reads zero or one when i switch to another cell with VoiceOver 问题是,当单元可重用时, accesibilityLabel
不起作用,并且当我使用VoiceOver切换到另一个单元时,VoiceOver读取零或一
How can I set value to accessibilityLabel when new cells are loaded? 加载新单元格后如何为accessibilityLabel设置值?
I attach my code 我附上我的代码
Celda.swift Celda.swift
class Celda: UITableViewCell {
@IBOutlet weak var label: UILabel!
func setup(_ number: String) {
label.text = number
}
override var isAccessibilityElement: Bool {
get {return false }
set { self.isAccessibilityElement = newValue}
}
private var _accessibilityElements: [Any]?
override var accessibilityElements: [Any]? {
get {
if let _accessibilityElements = _accessibilityElements {
return _accessibilityElements
}
var elements = [UIAccessibilityElement]()
let cellAccessibility = UIAccessibilityElement(accessibilityContainer: self)
cellAccessibility.accessibilityFrameInContainerSpace = self.contentView.frame
cellAccessibility.accessibilityLabel = label.text
cellAccessibility.isAccessibilityElement = true
elements.append(cellAccessibility)
_accessibilityElements = elements
return _accessibilityElements
}
set {
_accessibilityElements = newValue
}
}
}
ViewController.swift ViewController.swift
class ViewController: UIViewController {
var elementos = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"];
@IBOutlet weak var tabla: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tabla.dataSource = self
tabla.delegate = self
tabla.reloadData()
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return elementos.count;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let celda: Celda = tableView .dequeueReusableCell(withIdentifier: "Celda", for: indexPath) as! Celda
celda.setup(elementos[indexPath.row])
return celda
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 900.0
}
}
Can I set accessibilityLabel for the other cells? 我可以为其他单元格设置accessibilityLabel吗?
I need a solution scalable and that UIAccessibilityLabel
be the one who defines his own accessiblityLabel
我需要一种可扩展的解决方案,并且UIAccessibilityLabel
是定义自己的accessiblityLabel
的解决方案
You need to set it inside cellForRowAt
您需要在cellForRowAt
设置它
celda.setup(elementos[indexPath.row],value:"\(indexPath.row)")
func setup(_ number: String,value:String) {
label.text = number
label.accessibilityLabel = value
}
So every dequeue it's overwritten to the current indexPath 因此,每次出队都将其覆盖到当前indexPath
I need a solution scalable and that
UIAccessibilityLabel
be the one who defines his ownaccessiblityLabel
. 我需要一个可扩展的解决方案,并且UIAccessibilityLabel
是定义自己的accessiblityLabel
的解决方案。
To reach your goal, you need to reset all the cell properties before using it for new refreshed data: take a look at the prepareForReuse
table view cell function. 为了实现您的目标,您需要先重置所有单元格属性,然后再将其用于新的刷新数据:看看prepareForReuse
表视图单元格函数。
...for example, if I want to set isAccessibilityElement = false just for odd cells ...例如,如果我只为奇数单元格设置isAccessibilityElement = false
Insert the code snippets hereunder in Celda.swift first: 首先将以下代码段插入Celda.swift中 :
func setup(_ number: String, a11y isA11y: Bool) {
label.text = number
isAccessibilityElement = isA11y
}
override func prepareForReuse() {
super.prepareForReuse()
_accessibilityElements = nil
_isAccessibilityElement = false
label.text = "NADA"
}
private var _isAccessibilityElement: Bool?
override var isAccessibilityElement: Bool {
get { return _isAccessibilityElement ?? false }
set { _isAccessibilityElement = newValue }
}
private var _accessibilityElements: [Any]?
override var accessibilityElements: [Any]? {
get {
if let _accessibilityElements = _accessibilityElements {
return _accessibilityElements
}
var elements = [UIAccessibilityElement]()
let cellAccessibility = UIAccessibilityElement(accessibilityContainer: self)
cellAccessibility.accessibilityFrameInContainerSpace = self.contentView.frame
cellAccessibility.accessibilityLabel = label.text
cellAccessibility.isAccessibilityElement = self.isAccessibilityElement
elements.append(cellAccessibility)
_accessibilityElements = elements
return _accessibilityElements
}
set { _accessibilityElements = newValue }
}
... and in ViewController.swift : ...并在ViewController.swift中 :
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let celda: Celda = tableView .dequeueReusableCell(withIdentifier: "Celda",
for: indexPath) as! Celda
let isA11yCell = (Int(elementos[indexPath.row])! % 2) == 0 ? true : false
celda.setup(elementos[indexPath.row],
a11y: isA11yCell)
return celda
}
Can I set accessibilityLabel for the other cells? 我可以为其他单元格设置accessibilityLabel吗? I need a solution scalable... 我需要可扩展的解决方案...
This solution is scalable and it's up to you to adapt its content to your app environment. 该解决方案具有可扩展性,您可以根据自己的应用环境调整其内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.