ios - 为什么 XCTest 在 UITableViewCell 中公开一个随机的 otherElement? (并表现出其他不一致之处)

标签 ios objective-c uitableview xctest xcuitest

我创建了一个新的单 View 项目并添加了这段代码来创建一个 UITableView。


- (void)viewDidLoad {
    [super viewDidLoad];
    tableView=[[UITableView alloc]init];
    tableView.frame = CGRectMake(10,30,320,400);
    tableView.dataSource=self;
    tableView.delegate=self;
    tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [tableView reloadData];
    [self.view addSubview:tableView];
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier   forIndexPath:indexPath] ;
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = @"hello";
    cell.textLabel.accessibilityIdentifier = @"world";
    return cell;
}

我的 UI 测试只是使用 print(app.debugDescription) 打印 View 层次结构,如您所见,主窗口的图形有 2 个叶 View :StaticText对应于 cell.textLabel 和一个未识别/随机的 Other View 。我已经尝试了很多方法来弄清楚这个 Other View 是什么,但我找不到答案。


Element subtree:
 →Application, 0x600000d98820, pid: 17292, label: 'TestApp'
    Window (Main), 0x600000d989c0, {{0.0, 0.0}, {768.0, 1024.0}}
      Other, 0x600000d98a90, {{0.0, 0.0}, {768.0, 1024.0}}
        Table, 0x600000d98b60, {{10.0, 30.0}, {320.0, 400.0}}
          Cell, 0x600000d98c30, {{10.0, 30.0}, {320.0, 44.0}}
            StaticText, 0x600000d98d00, {{25.0, 30.0}, {290.0, 43.5}}, identifier: 'world', label: 'hello'
            Other, 0x600000d98dd0, {{25.0, 73.5}, {305.0, 0.5}}

也许更多的是this UITableViewCell odd/unexplained behavior

导致我这样做的另一个奇怪之处是当包含 UILabelUIScrollView 被添加到 UITableViewCell 时,UIScrollView 没有出现在层次结构中。所以这个:


- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier   forIndexPath:indexPath] ;
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 5,5)];
    [lb setText:@"hello world"];
    [sv addSubview:lb];
    [[cell contentView] addSubview:sv];
    return cell;
}

产生这个:

Element subtree:
 →Application, 0x6000014e75a0, pid: 20372, label: 'TestApp'
    Window (Main), 0x6000014e7190, {{0.0, 0.0}, {768.0, 1024.0}}
      Other, 0x6000014e6ff0, {{0.0, 0.0}, {768.0, 1024.0}}
        Table, 0x6000014e6f20, {{10.0, 30.0}, {320.0, 400.0}}
          Cell, 0x6000014e6e50, {{10.0, 30.0}, {320.0, 44.0}}
            StaticText, 0x6000014e6d80, {{10.0, 30.0}, {5.0, 5.0}}, label: 'hello world'
            Other, 0x6000014e6cb0, {{25.0, 73.5}, {305.0, 0.5}}

鉴于此(不涉及表格或单元格):

- (void)viewDidLoad {
    [super viewDidLoad];
    UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 5,5)];
    [lb setText:@"hello world"];
    [sv addSubview:lb];
    [self.view addSubview:sv];
}

似乎工作得很好:


Element subtree:
 →Application, 0x600000278750, pid: 20442, label: 'TestApp'
    Window (Main), 0x6000002788f0, {{0.0, 0.0}, {768.0, 1024.0}}
      Other, 0x600000278820, {{0.0, 0.0}, {768.0, 1024.0}}
        ScrollView, 0x6000002789c0, {{0.0, 0.0}, {10.0, 10.0}}
          StaticText, 0x600000278a90, {{0.0, 0.0}, {5.0, 5.0}}, label: 'hello world'

我的想法是,这在某种程度上与 isAccessibilityElementaccessibilityTraits 以及 View 是否为容器决定了暴露的内容有关,但不一致令人困惑我希望其他人知道这个 secret 公式。

最佳答案

这个其他元素是单元格之间的分隔线。 由于这些行不会影响其他元素,因此它们不应该打扰您。

当您使某些元素可访问时,其后代不会出现在 UI 测试元素树中。

关于ios - 为什么 XCTest 在 UITableViewCell 中公开一个随机的 otherElement? (并表现出其他不一致之处),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59441174/

相关文章:

ios - 发送到不可变对象(immutable对象)的变异方法关闭应用程序

android - 如何使用 Cordova/PhoneGap 跟踪地理位置?

objective-c - Cocoa 动画代理中的 NSCFString 内存泄漏

ios - 从矩形缩放 UIView 的正确转换?

ios - 警报 Swift 后重新加载表

ios - 使用 MPMediaQuery 使用 iPod 库中的视频填充 UITableView 时出现问题

iOS 自动保存的对象

ios - 红色录音条扭曲 View

objective-c - 图像在 UIImageView 中显示模糊

ios - 对 UITableViewCell 中的 UITableView 行执行操作以打开另一个 View Controller