0

我有一个相当复杂的 UITableViewCell 并且在生产代码中我可以使用以下内容提取选定的 UILabel

[[[[[[cell subviews] objectAtIndex:1] subviews] objectAtIndex:0] subviews] objectAtIndex:4];

一切正常 - 我遇到的问题是当我尝试创建一个简单的单元测试来显示预期如何工作时。

由于某种原因,我无法构建一个在单元格的 contentView 属性中有 2 个项目的场景

所以我可以很容易地得到 [[cell subviews] objectAtIndex:0] 但从来没有 1

在我的测试助手中,我正在执行以下操作来模拟这个(没有运气)

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
UIView *view1 = [[UIView alloc] init];
UILabel *label1 = [[UILabel alloc] init];
UIView *view2 = [[UIView alloc] init];
UILabel *label2 = [[UILabel alloc] init];

[view1 addSubview:label1];
[cell.contentView addSubview:view1];

[view2 addSubview:label1];
[view2 addSubview:label2];
[cell.contentView addSubview:view2];

NSLog(@"inside ...%@", [[cell subviews] objectAtIndex:1]);
4

1 回答 1

0

您不能将 label1 添加到 view1 AND view2,您必须创建两个不同的标签。一个视图只能有一个超级视图,因此当您说[view2 addSubview:label1];label1 从 view1 中删除时。

于 2012-04-19T12:28:12.863 回答