【发布时间】:2013-02-08 09:28:41
【问题描述】:
我正在为我的应用设计一个自定义 UIView。
UIView 将包含以下组件:
- UISearchbar
- UITableView
我的初始化程序如下:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_searchBar = [[UISearchBar alloc]initWithFrame:CGRectZero];
_tableView = [[UITableView alloc]initWithFrame:CGRectZero];
_tableView.dataSource = self;
[super addSubView:_searchBar];
[super addSubView:_tableView];
// Initialization code
}
return self;
}
我打算在layoutsubviews方法中设置_searchBar和_tableView的frame。
但我认为我将 _tableView 添加到 super 的方式是错误的。因为在 _tableView 添加到子视图的那一刻,_tableView 的数据源方法就会被触发。这甚至发生在自定义类本身的创建之前。
这是一个正确的设计吗?
我可以像下面这样在 layoutSubviews 中单独添加 _tableView 吗?
-(void)layoutSubViews{
//Adjust frame
[_tableView removeFromSuperView];
[self addSubView:_tableView];
}
【问题讨论】:
标签: ios uiview customization layoutsubviews