首页 > 解决方案 > 如何使用 Voice Over 确定当前聚焦的 UITableViewCell 的索引路径?

问题描述

我有一个动态的 UITableView。对于每个单元格,我添加一个UIAccessibilityCustomAction. 当动作触发时,我需要知道索引路径,这样我才能做出相应的响应并更新我的模型。

tableView(_:cellForRowAt:)我添加我UIAccessibilityCustomAction这样的...

cell.accessibilityCustomActions = [
    UIAccessibilityCustomAction(
        name: "Really Bad Name",
        target: self,
        selector: #selector(doSomething)
    )
]

我试过用UIAccessibility.focusedElement没用...

@objc private func doSomething() {
    let focusedCell = UIAccessibility.focusedElement(using: UIAccessibility.AssistiveTechnologyIdentifier.notificationVoiceOver) as! UITableViewCell
    // Do something with the cell, like find the indexPath.
}

问题是投射到单元格失败。调试器说返回值类型实际上是 a UITableTextAccessibilityElement,我找不到任何信息。

标签: iosuitableviewuikitvoiceover

解决方案


当动作触发时,我需要知道索引路径,这样我才能做出相应的响应并更新我的模型。

达到目标的最佳方法是使用UIAccessibilityFocus 非正式协议方法,直接在对象中覆盖它们(在您的情况下为表格视图单元格类):当触发自定义操作时,您将能够捕获所需的索引路径.

我建议看一下这个处理捕获可访问性焦点更改的答案,如果需要,它包含带有代码片段的详细解决方案。

示例片段...

class SomeCell: UITableViewCell

    override open func accessibilityElementDidBecomeFocused() {
        // Notify view controller however you want (delegation, closure, etc.)
    }

}

推荐阅读