【发布时间】:2021-05-06 20:44:20
【问题描述】:
有什么方法可以让 tableview 读取为可访问性列表,同时将全部注意力集中在 tableview 上? 例如:我有一个类似的列表
- 艺术
- 球
- 汽车
- 狗
所以我希望可访问性阅读器阅读为“第 1 项,共 4 项艺术,第 2 项,共 4 项球,......等等”
【问题讨论】:
有什么方法可以让 tableview 读取为可访问性列表,同时将全部注意力集中在 tableview 上? 例如:我有一个类似的列表
所以我希望可访问性阅读器阅读为“第 1 项,共 4 项艺术,第 2 项,共 4 项球,......等等”
【问题讨论】:
是的,你可以,但你必须手动实现。
您可以为您的单元创建某种模型,用于配置它。 您需要将表格视图的总行数传递给每个单元格的配置。
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()
}
【讨论】: