首页 > 解决方案 > 无障碍列表读取 iOS Swift

问题描述

有什么方法可以让 tableview 在将全部注意力集中在 tableview 上的同时读取为可访问性列表?例如:我有一个类似的列表

  1. 艺术

因此,我希望可访问性阅读器阅读为“第 1 项,共 4 项艺术,第 2 项,共 4 项球,......等”

标签: iosswiftswift3

解决方案


是的,你可以,但你必须手动实现。

您可以为您的单元创建某种模型,用于配置它。您需要将表格视图的总行数传递给每个单元格的配置。

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()
    }

推荐阅读