我正在处理一个项目,我正在尝试将 TableView 标题设置为一个复杂的 View 。该项目不使用自动布局,所有约束均以编程方式处理。我有一个包含 TableView 的父 View Controller 。
这是父 View Controller 的代码(委托(delegate)和数据源在父 View 中)
@property (nonatomic, strong) NSMutableArray *array;
- (void)viewDidLoad
{
[super viewDidLoad];
self.array = [NSMutableArray new];
[self.array addObject:@"One"];
[self.array addObject:@"Two"];
[self.array addObject:@"Three"];
[self.array addObject:@"Four"];
[self.view addSubview:self.detailedTableView];
}
- (void)updateViewConstraints
{
[super updateViewConstraints];
NSDictionary *views = @{
@"detailedTable": self.detailedTableView,
};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[detailedTable]-0-|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[detailedTable]-0-|" options:0 metrics:nil views:views]];
constant:0.f]];
}
-(UITableView *)detailedTableView
{
if(!_detailedTableView)
{
_detailedTableView = [UITableView new];
_detailedTableView.dataSource = self;
_detailedTableView.delegate = self;
_detailedTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_detailedTableView.showsVerticalScrollIndicator = NO;
_detailedTableView.separatorInset = UIEdgeInsetsZero;
_detailedTableView.translatesAutoresizingMaskIntoConstraints = NO;
}
return _detailedTableView;
}
在从父级继承的 subview Controller 中,我试图将标题 View 设置为包含在 subview Controller 中的自定义 View 。相关代码如下:
@interface ChildViewController ()
@property (nonatomic, strong) UIView *testView;
@end
@implementation ChildViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.detailedTableView.tableHeaderView = self.testView;
}
-(UIView *)testView
{
if(!_testView)
{
_testView = [UIView new];
_testView.backgroundColor = [UIColor orangeColor];
_testView.translatesAutoresizingMaskIntoConstraints = NO;
}
return _testView;
}
- (void)updateViewConstraints
{
[super updateViewConstraints];
NSDictionary *views = @{
@"test" : self.testView
};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[test(width)]-0-|" options:0 metrics:metrics views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[test(100)]" options:0 metrics:metrics views:views]];
}
我不是通过框架属性设置宽度或高度,而是通过约束。然而,此代码在 iOS 8.1 上生成下图并在 iOS 7.1 上完全崩溃
我可以看到正在添加虚拟 View ,但它不在适当的位置作为 tableHeaderView,因为它覆盖了 UITable 中的其他数组项。另外,这在 iOS 7.1 上不起作用,因为它会抛出此错误消息:
*** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2935.137/UIView.m:8794
2015-01-03 22:33:05.540 Saloote A[10073:607] This is where we save the application data during a exception
2015-01-03 22:33:05.541 Saloote A[10073:607] Exception: Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.
最佳答案
出现这种情况是因为您的测试 View 不是 vc.view 的直接 subview 。
您只能为 subview 添加约束。
您可以使用 setFrame 而不是 autoLayout。
关于ios - 设置自定义 UITableView tableHeaderView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27762107/