我有一个对象数组并在滚动视图中显示它们。如果用户在该数组中添加或删除模型,我想重新加载滚动视图。
我正在使用iOS6发行说明中描述的混合方法
- (void)layoutScrollView
{
[self clearScrollView]; // loop through scroll view's subviews and call remove from super view
int contentHeight = (BANQUET_TICKET_HEIGHT * self.banquetList.count) + ADD_BUTTON_HEIGHT + 60;
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, contentHeight)];
containerView.backgroundColor = [UIColor blueColor];
[self.scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.scrollView addSubview:containerView];
[self.scrollView setContentSize:CGSizeMake(300, contentHeight)];
CGFloat originY = 0;
for (int i = 0; i < self.banquetList.count; i ++) // add tickets
{
UIView *ticket = [[UIView alloc] init];
[containerView addSubview:ticket];
[ticket setTranslatesAutoresizingMaskIntoConstraints:NO];
NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:ticket
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeWidth
multiplier:1
constant:BANQUET_TICKET_WIDTH];
NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:ticket
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeHeight
multiplier:1
constant:BANQUET_TICKET_HEIGHT];
[ticket addConstraints:@[width, height]];
NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:ticket
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:containerView
attribute:NSLayoutAttributeLeading
multiplier:1
constant:0];
NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:ticket
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:containerView
attribute:NSLayoutAttributeTop
multiplier:1
constant:originY];
[containerView addConstraints:@[leading, top]];
originY += 150;
}
if ([containerView hasAmbiguousLayout])
{
[containerView exerciseAmbiguityInLayout];
NSLog(@"hasAmbiguousLayout");
}
}
它只在我第一次在viewDidLoad中调用此方法时才有效。但是,如果我修改数组并再次调用此方法,hasAmbiguousLayout
将返回YES。它不会崩溃应用程序,故障单视图显示,但滚动视图将无法正确滚动,它无法滚动到最顶部或最底部的内容,只显示其内容的一部分。像内容大小的内容已经改变了。
实现这一目标的正确方法是什么?有人可以帮帮我吗?
感谢。