首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可访问性看不到SWTableViewCell的实用程序按钮

可访问性看不到SWTableViewCell的实用程序按钮
EN

Stack Overflow用户
提问于 2015-10-08 09:54:14
回答 2查看 345关注 0票数 3

我使用的是SWTableviewCell,我可以向左滑动,显示带有XCTest和可访问性的实用程序按钮,但找不到这些元素。与无障碍检查人员进行了双重检查。

由于SWTableViewCell中的实用程序按钮是常规的UIButtons,所以它应该是开箱即用的。还尝试设置按钮的辅助功能标签。

任何帮助都将不胜感激。谢谢!

更新:这是我的代码:

代码语言:javascript
复制
    // This works
    app = XCUIApplication()
    let firstCell = app.tables.cells.elementBoundByIndex(0)
    firstCell.swipeLeft()

    // Following 2 TODO's doesn't work
    // TODO: tap More button
    firstCell.buttons["More"].tap()
    XCTAssertEqual(app.sheets.count, 1, "'More' action sheet should be present")
    // TODO: assert sheet doesn't contain Lock/Unlock button
    let sheet = app.sheets.element
    let lockButton = sheet.buttons["Lock"]
    sheet.buttons["Cancel"].tap()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-26 08:53:16

最后重写我的UIAccessibilityContainer子类的UITableViewCell方法,如下所示:

代码语言:javascript
复制
- (NSArray *)accessibilityElements {
    if (_accessibilityElements == nil) {
        _accessibilityElements = [[NSMutableArray alloc] initWithCapacity: 2];
        [_accessibilityElements addObject: self.miscLabel];
        [_accessibilityElements addObject: self.titleLabel];
    }
    return _accessibilityElements;
}   

- (nullable id)accessibilityElementAtIndex:(NSInteger)index {
    return _accessibilityElements[index];
}   

- (NSInteger)indexOfAccessibilityElement:(id)element {
    return [_accessibilityElements indexOfObject: element];
}

_accessibilityElements是我的单元格的实例变量:

代码语言:javascript
复制
@interface MyCell : SWTableViewCell
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *miscLabel;
...
@property (strong, nonatomic) NSMutableArray *accessibilityElements;

@end

然后,实现SWTableViewCellDelegate方法来向可访问性添加和删除实用程序按钮:

代码语言:javascript
复制
- (void)swipeableTableViewCellDidEndScrolling:(MyCell *)cell {
    NSMutableArray *utilityButtons = cell.accessibilityElements;
    if (cell.isUtilityButtonsHidden) {
        [utilityButtons removeObjectsInArray: cell.rightUtilityButtons];
    }
    else {
        [utilityButtons addObjectsFromArray: cell.rightUtilityButtons];
    }
}
票数 1
EN

Stack Overflow用户

发布于 2016-03-02 15:05:13

在SWTableViewCell.h中,在文件末尾添加以下代码行。

代码语言:javascript
复制
// Accessibility
@property (strong, nonatomic) NSMutableArray *accessibilityElements;
- (void)addElementForAccesiblilty:(id)element;
- (void)removeElementFromAccesiblilty:(id)element;

以及SWTableViewCell.m中的以下行

代码语言:javascript
复制
#pragma mark Accessibilty

- (void)addElementForAccesiblilty:(id)element
{
    if (![self.accessibilityElements containsObject:element]) {
        [self.accessibilityElements addObject:element];
    }
}

- (void)removeElementFromAccesiblilty:(id)element
{
    [self.accessibilityElements removeObject:element];
}

- (NSArray *)accessibilityElements {
    if (!_accessibilityElements) {
        _accessibilityElements = [[NSMutableArray alloc] initWithCapacity: 0];
        [self updateAccessibilityElementsForView:self];
    }
    return _accessibilityElements;
}

- (void)updateAccessibilityElementsForView:(UIView*)v
{
    for (UIView *subView in v.subviews)
    {
        if ([subView isKindOfClass:[UILabel class]] || [subView isKindOfClass:[UIButton class]]) {
            [_accessibilityElements addObject:subView];
        }
        [self updateAccessibilityElementsForView:subView];
    }
}

- (nullable id)accessibilityElementAtIndex:(NSInteger)index {
    id element = nil;
    @synchronized(_accessibilityElements) {
        element = _accessibilityElements[index];
    }
    return element;
}

- (NSInteger)indexOfAccessibilityElement:(id)element {
    NSInteger index = NSNotFound;
    @synchronized(_accessibilityElements) {
        index = [_accessibilityElements indexOfObject: element];
    }
    return index;
}


- (void)updateAccessibleElementsForUtilityButtons {
    switch (self.cellState) {
        case kCellStateLeft:
        {
            if (self.isUtilityButtonsHidden) {
                [self.accessibilityElements removeObjectsInArray: self.leftUtilityButtons];
            }
            else {
                [self.accessibilityElements addObjectsFromArray: self.leftUtilityButtons];
            }
            break;
        }
        case kCellStateRight:
        {
            if (self.isUtilityButtonsHidden) {
                [self.accessibilityElements removeObjectsInArray: self.rightUtilityButtons];
            }
            else {
                [self.accessibilityElements addObjectsFromArray: self.rightUtilityButtons];
            }
            break;
        }
        default: {
            [self.accessibilityElements removeObjectsInArray: self.rightUtilityButtons];
            [self.accessibilityElements removeObjectsInArray: self.leftUtilityButtons];
            break;
        }
    }
}

scrollViewDidEndScrollingAnimationscrollViewDidEndDecelerating委托方法中调用方法scrollViewDidEndDecelerating如下所示

代码语言:javascript
复制
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self updateCellState];
    [self updateAccessibleElementsForUtilityButtons];
    if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableTableViewCellDidEndScrolling:)]) {
        [self.delegate swipeableTableViewCellDidEndScrolling:self];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self updateCellState];
    [self updateAccessibleElementsForUtilityButtons];
    if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableTableViewCellDidEndScrolling:)]) {
        [self.delegate swipeableTableViewCellDidEndScrolling:self];
    }
}

这将添加所有按钮和标签作为单元格中的可访问元素。但是,如果要添加/删除任何元素用户,请使用以下方法

代码语言:javascript
复制
- (void)addElementForAccesiblilty:(id)element;
- (void)removeElementFromAccesiblilty:(id)element;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33012382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档