问题描述
我将UITableView作为子视图添加到我正在处理的自定义UIView类中。但是我注意到每当我滚动表时它会调用我的类layoutSubviews。我很确定UIScrollview表是继承了实际执行此操作的表,但想知道是否有办法禁用此功能,如果不是为什么会发生这种情况?我不明白为什么当你滚动一个scrollview时,它需要超级视图来布局它的子视图。
I added a UITableView as a subview to a custom UIView class I'm working on. However I noticed that whenever I scroll the table it calls my classes layoutSubviews. I'm pretty sure its the UIScrollview that the table is inheriting from which is actually doing this but wanted to know if there is a way to disable this functionality and if not why is it happening? I don't understand why when you scroll a scrollview it needs its superview to layout its subviews.
代码:
@implementation CustomView
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
self.clipsToBounds = YES;
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 15.0, 436.0, 132.0) style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
tableView.backgroundColor = [UIColor clearColor];
tableView.showsVerticalScrollIndicator = NO;
tableView.contentInset = UIEdgeInsetsMake(kRowHeight, 0.0, kRowHeight, 0.0);
tableView.tag = componentIndex;
[self addSubview:tableView];
[tableView release];
}
return self;
}
- (void)layoutSubviews {
// This is called everytime I scroll the tableview
}
@end
推荐答案
是的, UIScrollView在滚动时会调用layoutsubviews。我本来可以发誓,这在文档的某个地方有说明,但我猜不是。
Yes, a UIScrollView does call layoutsubviews whenever it scrolls. I could've sworn this was stated in the documentation somewhere, but I guess not.
无论如何,流行的想法是UIScrollView应该布局它的东西,以便不应该列出目前看不见的观点。当用户在滚动视图中滚动时,它应根据需要添加和删除子视图。我猜这是TableViews用来排队隐藏的表格单元格。
Anyways, the prevailing idea for this is that a UIScrollView should layout its stuff so that views that currently can't be seen shouldn't be laid out. As users scroll in the scroll view, it should add and remove subviews as necessary. I'm guessing this is what TableViews use to enqueue table cells that get hidden.
你是否有理由关注layoutsubviews是否被调用?
Is there any reason why you would care if layoutsubviews gets called or not?
这篇关于滚动时UIScrollview调用superviews layoutSubviews?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!