【问题标题】:Cant access TableViewCell elements using UI Automation无法使用 UI 自动化访问 TableViewCell 元素
【发布时间】:2011-05-19 06:50:15
【问题描述】:

我有一个显示电话会议详细信息的自定义表格视图单元格。单元格构造函数的主体如下:

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString*) reuseIdentifier {
 if(self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]) {
      self.contentView.isAccessibilityElement = YES;
      self.contentView.accessibilityLabel = @"Blah";

      conferenceNameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
      conferenceNameLabel.adjustsFontSizeToFitWidth = YES;
      conferenceNameLabel.font = [UIFont systemFontOfSize:14];
      conferenceNameLabel.isAccessibilityElement = YES;
      conferenceNameLabel.accessibilityLabel = @"Name";
      [self.contentView addSubview:conferenceNameLabel];

      conferenceDateLabel = [[UILabel alloc] initWithFrame:CGRectZero];
      conferenceDateLabel.adjustsFontSizeToFitWidth = YES;
      conferenceDateLabel.font = [UIFont systemFontOfSize:14];
      conferenceDateLabel.isAccessibilityElement = YES;
      conferenceDateLabel.accessibilityLabel = @"Date";
      [self.contentView addSubview:conferenceDateLabel];

      recurringIconView = [[UIImageView alloc] initWithFrame:CGRectZero];
      [recurringIconView setContentMode:UIViewContentModeScaleAspectFit];
      recurringIconView.isAccessibilityElement = YES;
      recurringIconView.accessibilityLabel = @"Icon";
      [self.contentView addSubview:recurringIconView];

      [self setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
 }

 return self;

}

使用 UI 自动化时,我可以使用可访问性标签“Blah”访问内容视图。但我无法访问我的任何标签或 ImageView。为什么我无法使用 UI 自动化处理这些 UI 元素?

【问题讨论】:

    标签: iphone uitableview automation ios4


    【解决方案1】:

    在您的脚本中,您应该能够使用 logElementTree() 注销每个单元格的可访问性层次结构:

    cell.logElementTree();
    

    这些子视图应连同它们的可访问性标签一起出现在该记录树中。

    如果您无法通过它们的标签来处理这些元素,您应该能够通过它们在同级视图列表中的相对位置来获取它们:

    var firstLabel = cell.elements()[0];
    

    我在我的course on iTunes U 中介绍了 UI 自动化并展示了如何测试此类表格视图的示例。

    【讨论】:

    • 我得看看你的课程资料,也许会有一个“啊哈时刻”,我会意识到我在做一些傻事。
    • 我曾尝试记录元素树,但 UI 自动化报告我的单元格有一个长度为 1 的元素列表,并且该单个元素是一个 UIAElement,其名称是电话会议的名称和日期,并且有一个null 的值。
    • @Jonathan - 这可能是因为表格单元格看起来是可访问性容器:developer.apple.com/library/ios/#documentation/UserExperience/…。您可能需要覆盖自定义表格单元格中的 UIAccessibilityContainer 协议方法,以返回正确数量的标签和这些标签的数组。但是请注意,这可能会降低您的应用程序对视障用户的可访问性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 2016-02-29
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 2018-03-08
    • 2014-08-10
    相关资源
    最近更新 更多