[英]Testing UITableView's section footer
Trying to test a UISegmentedControl in a UITableView, which is created in the UITableViewDelegate method: 尝试在由UITableViewDelegate方法创建的UITableView中测试UISegmentedControl:
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if (section == 0) {
UIView *container = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, [self tableView:tableView heightForFooterInSection:section])];
[self.segmentedControl setCenter:container.center];
[container addSubview:self.segmentedControl];
return container;
} else {
return [super tableView:tableView viewForFooterInSection:section];
}
}
In the test class: 在测试课中:
-(void)testSegmentedControl {
MyTableViewController *viewController = [[MyTableViewController alloc]initWithNibName:@"MyTableViewController" bundle:nil];
[viewController.tableView reloadData];
// Getting the footer via the delegate is cheating IMO.
UITableViewHeaderFooterView *footer = [viewController.tableView footerViewForSection:0];
UISegmentedControl *segmentControl = footer.subviews[0];
// do stuff to the segmentControl then check the tableView.
[viewController.tableView reloadData];
}
I'm currently manipulating the UISegmentedControl
via a global property ( viewController.segmentedControl
) and then calling [viewController.tableView reloadData]
to update the state of the UITableViewCell
s. 我目前正在通过全局属性(
viewController.segmentedControl
)操纵UISegmentedControl
,然后调用[viewController.tableView reloadData]
更新UITableViewCell
的状态。 But it seems to me that getting the footer from [viewController.tableView footerViewForSection:0]
is the correct way to test. 但是在我看来,从
[viewController.tableView footerViewForSection:0]
获取页脚是正确的测试方法。 Any guidance is appreciated. 任何指导表示赞赏。
Edit: 编辑:
As suggested by John Rodgers, I've tried dequeuing a UITableViewHeaderFooterView like we'd do in cellForRowAtIndexPath:
正如约翰·罗杰斯(John Rodgers)所建议的那样,我已经尝试像在
cellForRowAtIndexPath:
那样使UITableViewHeaderFooterView出队cellForRowAtIndexPath:
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if (section == 0) {
UITableViewHeaderFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:self.footerReuseId];
if (footer == nil) {
footer = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:self.footerReuseId];
}
if (![footer.subviews containsObject:self.segmentedControl]) {
[footer addSubview:self.segmentedControl];
}
return footer;
} else {
return [super tableView:tableView viewForFooterInSection:section];
}
}
No improvement in the test method: 测试方法无改进:
-(void)testSegmentedControl {
MyTableViewController *viewController = [[MyTableViewController alloc]initWithNibName:@"MyTableViewController" bundle:nil];
[viewController.tableView reloadData];
UITableViewHeaderFooterView *footer1 = (UITableViewHeaderFooterView *)[viewController.tableView dequeueReusableHeaderFooterViewWithIdentifier:viewController.footerReuseId];
UITableViewHeaderFooterView *footer2 = [viewController.tableView footerViewForSection:0];
// Break point shows that footer1 and footer2 are nil.
}
The problem you're having here, is that footerViewForSection
is actually a UITableView method that's used provide it with the view for that section. 您在这里遇到的问题是,
footerViewForSection
实际上是一个UITableView方法,用于为其提供该部分的视图。
You're going to have to dequeue a reusable UITableViewHeaderFooterView in order to test it in your test class: 您将必须使可重用的UITableViewHeaderFooterView出队,以便在测试类中对其进行测试:
UITableViewHeaderFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:mySectionFooterViewIdentifier];
This is exactly what you should be using in your footerViewForSection
method you provide for your table view, you're just dequeueing it in the test class instead. 这正是为表视图提供的
footerViewForSection
方法中应该使用的方法,而只是在测试类中将其出队。
Hope this helps! 希望这可以帮助!
~ J 〜J
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.