ios - 如何使用 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
,我找不到任何信息。
解决方案
当动作触发时,我需要知道索引路径,这样我才能做出相应的响应并更新我的模型。
达到目标的最佳方法是使用UIAccessibilityFocus 非正式协议方法,直接在对象中覆盖它们(在您的情况下为表格视图单元格类):当触发自定义操作时,您将能够捕获所需的索引路径.
我建议看一下这个处理捕获可访问性焦点更改的答案,如果需要,它包含带有代码片段的详细解决方案。
示例片段...
class SomeCell: UITableViewCell
override open func accessibilityElementDidBecomeFocused() {
// Notify view controller however you want (delegation, closure, etc.)
}
}
推荐阅读
- python - 替换字符串 - 位置上的未知标志
- java - 如何使用 android 中的共享首选项更新列表 recyclerview?
- python - 存储用户偏好的 GUI 程序 - Python 课程简介
- asp.net-core - iis反向代理404
- swift - iOS UI 自动化:文本字段的断言错误(以红色突出显示)通过 CTAssertTrue
- python - 如何在 Python 单元测试中修补用于内部方法的类?
- android - IllegalStateException: Fragment DetailFragment 没有从 onCreateView() 返回视图,或者在 onCreateView() 之前调用了该视图
- java - 我想从循环中输出一次
- javascript - 组件上 Material UI 的替代颜色
- javascript - 如何修复 AMP 页面中的“不允许使用标签 pwa”?