问题在于,当单元可重用时,accesibilityLabel
不起作用,并且当我使用VoiceOver切换到另一个单元时,VoiceOver会读取 zero 或 one / p>
加载新单元格后,如何为accessibilityLabel设置值?
我附上我的代码
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
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
}
}
我可以为其他单元格设置accessibilityLabel吗?
我需要一个可扩展的解决方案,让UIAccessibilityLabel
是定义自己的accessiblityLabel
的人
答案 0 :(得分:2)
您需要将其设置在cellForRowAt
celda.setup(elementos[indexPath.row],value:"\(indexPath.row)")
func setup(_ number: String,value:String) {
label.text = number
label.accessibilityLabel = value
}
因此,每次出队都将其覆盖到当前indexPath
答案 1 :(得分:0)
我需要一个可扩展的解决方案,让
UIAccessibilityLabel
是定义自己的accessiblityLabel
的人。
要实现您的目标,您需要先重置所有单元格属性,然后再将其用于新的刷新数据:看看prepareForReuse
表格视图单元格功能。
...例如,如果我想仅将奇数单元格设置为isAccessibilityElement = false
首先在 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 }
}
...以及 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
}
我可以为其他单元格设置accessibilityLabel吗?我需要一个可扩展的解决方案...
此解决方案具有可扩展性,您可以根据自己的应用环境调整其内容。