我遇到了我认为有点像新手的问题......我制作了一个动态滚动视图,其中图像和标签以编程方式添加为子视图。问题是只显示我添加为子视图的最后一个。此外,当我读到“addSubview:”时,它会说“将视图添加到接收者的子视图列表的末尾”。这是否意味着只显示最后添加的子视图?在那种情况下,我如何使两者都可见?
在此先感谢,汤姆
代码:
for(int i = 0; i < [famorableArray count]; i++){
UIButton *famorableButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[famorableButton setFrame:CGRectMake(0.0f, 5.0f, 57.0f, 57.0f)];
[famorableButton setImage:personLogo forState:UIControlStateNormal];
NSString *famString = [NSString stringWithFormat:@"%@", [[[famorableArray objectAtIndex:i] substringFromIndex:8] capitalizedString]];
NSLog(@"%@", famString);
UILabel *famLabel = [[UILabel alloc] initWithFrame:CGRectZero];
famLabel.text = famString;
NSLog(@"Test2 %@", famLabel.text);
// Move the buttons position in the x-demension (horizontal).
CGRect btnRect = famorableButton.frame;
btnRect.origin.x = totalButtonWidth;
[famorableButton setFrame:btnRect];
CGRect labelRect = famLabel.frame;
labelRect.origin.x = totalButtonWidth + 28.5f;
[famLabel setFrame:btnRect];
// Add the button to the scrollview
[famScroll addSubview:famLabel];
[famScroll addSubview:famorableButton];
// Add the width of the button to the total width.
totalButtonWidth += famorableButton.frame.size.width + 30;
}
[famScroll setContentSize:CGSizeMake(totalButtonWidth, 79.0f)];