【问题标题】:Incorrect accessibility order in UICollectionViewCell with viewForSupplementaryElement带有 viewForSupplementaryElement 的 UICollectionViewCell 中的可访问性顺序不正确
【发布时间】:2014-10-14 20:01:13
【问题描述】:

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

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

这是什么原因造成的?

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

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

【问题讨论】:

    标签: ios uicollectionview uiaccessibility


    【解决方案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 crash (message sent to deallocated instance)

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-27
        • 1970-01-01
        • 1970-01-01
        • 2016-04-04
        • 2018-12-06
        • 1970-01-01
        • 2021-07-30
        • 1970-01-01
        相关资源
        最近更新 更多