问题描述
有什么方法可以让 tableview 读取为可访问性列表,同时将整个重点放在 tableview 上? 例如:我有一个像
这样的列表- 艺术
- 球
- 汽车
- 狗
所以我希望无障碍阅读器阅读为“4 项艺术的第 1 项,4 个球的第 2 项,......等”
解决方法
是的,您可以,但您必须手动实现。
您可以为您的单元创建某种模型,用于配置它。 您需要将表格视图的总行数传递给每个单元格的配置。
struct CellConfig {
let title: String
private let count: Int
init(title: String,count: Int) {
self.title = title
self.count = count
}
}
您实际上可以扩展功能,通过像这样传递当前的 CellConfig
来让 IndexPath
返回正确的无障碍标签:
struct CellConfig {
...
func axLabel(for indexPath: IndexPath) -> String {
let currentElement = indexPath.row + 1
return "Item \(currentElement) of \(count). \(title)."
}
}
因此,当从您的委托方法返回您的单元格时:
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard indexPath.row < items.count else { return UITableViewCell() }
let item = items[indexPath.row] // The array here holds all the configs of every cell.
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell",for: indexPath) as? UITabelViewCell
cell?.titleLabel.text = item.title
cell?.accessibilityLabel = item.axLabel(for: indexPath)
return cell ?? UITableViewCell()
}