我使用的是SWTableviewCell,我可以向左滑动,显示带有XCTest和可访问性的实用程序按钮,但找不到这些元素。与无障碍检查人员进行了双重检查。
由于SWTableViewCell中的实用程序按钮是常规的UIButtons,所以它应该是开箱即用的。还尝试设置按钮的辅助功能标签。
任何帮助都将不胜感激。谢谢!
更新:这是我的代码:
// 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()发布于 2016-02-26 08:53:16
最后重写我的UIAccessibilityContainer子类的UITableViewCell方法,如下所示:
- (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是我的单元格的实例变量:
@interface MyCell : SWTableViewCell
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *miscLabel;
...
@property (strong, nonatomic) NSMutableArray *accessibilityElements;
@end然后,实现SWTableViewCellDelegate方法来向可访问性添加和删除实用程序按钮:
- (void)swipeableTableViewCellDidEndScrolling:(MyCell *)cell {
NSMutableArray *utilityButtons = cell.accessibilityElements;
if (cell.isUtilityButtonsHidden) {
[utilityButtons removeObjectsInArray: cell.rightUtilityButtons];
}
else {
[utilityButtons addObjectsFromArray: cell.rightUtilityButtons];
}
}发布于 2016-03-02 15:05:13
在SWTableViewCell.h中,在文件末尾添加以下代码行。
// Accessibility
@property (strong, nonatomic) NSMutableArray *accessibilityElements;
- (void)addElementForAccesiblilty:(id)element;
- (void)removeElementFromAccesiblilty:(id)element;以及SWTableViewCell.m中的以下行
#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;
}
}
}在scrollViewDidEndScrollingAnimation和scrollViewDidEndDecelerating委托方法中调用方法scrollViewDidEndDecelerating如下所示
- (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];
}
}这将添加所有按钮和标签作为单元格中的可访问元素。但是,如果要添加/删除任何元素用户,请使用以下方法
- (void)addElementForAccesiblilty:(id)element;
- (void)removeElementFromAccesiblilty:(id)element;https://stackoverflow.com/questions/33012382
复制相似问题