3

我有一个 UITableViewCell,它在每一行中都包含一个 UICollectionViewCell。此集合视图具有显示在单元格顶部的补充视图(标题)。

在启用 VoiceOver 的情况下导航时,以正确的顺序读取单元格(标题、collectionviewcell1、collectionviewcell2,...)但是,在视图滚动后(由于在单元格中从左向右滑动),顺序会被破坏,读出UICollectionView 单元格,然后是标题。

什么可能导致这种情况?

我尝试在包含 UITableViewCell 上使用 UIAccessibilityContainer 协议,并返回 UICollectionView 中的项目数,加上标题,以及返回索引 0 处的标题和给定索引处的 UICollectionViewCell 的索引。这总是首先突出显示标题,但不会浏览 UICollectionView 单元格。

我还尝试将计数返回为 2 个元素(标题和 UICollectionView),并将这些对象返回为可访问性元素AtIndex。这确实从标题开始,但只读出 CollectionView 中的第一项

4

2 回答 2

1

事实证明,不正确的排序是由使用 UITableViewCell 作为 UICollectionView 的标题视图引起的。打开 VoiceOver 滚动时出现崩溃,我设法通过在 UIAccessibilityContainer 协议中返回单元格的 contentView 来阻止此崩溃。

#pragma mark - UIAccessibilityContainer

-(NSInteger)accessibilityElementCount {

    int headerCount = self.headerView ? 1 : 0;
    int footerCount = self.footerView ? 1 : 0;

    return ([self.dataValues count] + headerCount + footerCount;
}

-(id)accessibilityElementAtIndex:(NSInteger)index {
    if (index == 0) {
        if (self.headerView) {
            if ([self.headerView isKindOfClass:[UITableViewCell class]]) {
                UIView *header = ((UITableViewCell *)self.headerView).contentView;
                header.shouldGroupAccessibilityChildren = YES;
                return header;
            }
            return self.headerView;
        }
    }
    if (self.headerView) index -= 1;

    if (index >= [self.dataValues count]) {
        if ([self.footerView isKindOfClass:[UITableViewCell class]]) {
            return ((UITableViewCell *)self.footerView).contentView;
        }
        return self.footerView;
    }

    return [self collectionView:_collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
}

这个问题的答案为我指明了正确的方向:

iOS VoiceOver 崩溃(发送到已释放实例的消息)

于 2014-08-25T08:14:43.503 回答
1

在 iOS 11 上测试的 Swift 版本解决方案。

override func accessibilityElementCount() -> Int {
    return cellViewModels.count
}

override func accessibilityElement(at index: Int) -> Any? {
    return collectionView.cellForItem(at: IndexPath(item: index, section: 0))
}

注意:使用collectionView.cellForIteminfunc accessibilityElement(at index: Int) -> Any?获取当前集合视图单元格而不是func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell委托。这是与daentech解决方案的区别。

于 2019-06-11T20:13:43.573 回答