gpt4 book ai didi

ios - ScrollView 仅显示最后添加的 subview

转载 作者:可可西里 更新时间:2023-11-01 03:33:09 25 4
gpt4 key购买 nike

我对 UIScrollView subview 有奇怪的行为,我的想法是以编程方式创建一个 UIView 的实例,其中包含一个自定义的 nib 文件,在我的例子中是一个表单,填写用模型类中的数据形成,并将其添加为我的 UIScrollView 的 subview 。 问题 是当我处理多个 subview 时,UIScrollView 只保留最新的 subview ,所以如果我创建了三个 subview , ScrollView 将只显示第三个(最新) subview 。尽管页面控件设置为正确的 subview 数(三个)。

项目太长,所以我会尝试用相关代码简要说明我的问题:

- (void)viewDidLoad {

NSArray *sorted = [appArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];//this array contains the data from model, I debugged that to make sure I got the exact data, no more no less :)


//Loop all NSManaged objects, for example let's say I have 3 model objects, don't worry about how I get data etc, because I debugged all and maked sure all data objects number are exact, etc.
for (App_Table *_appTable in sorted) {
//This looped 3 times as expected, I debugged that also and maked sure on each iteration I got the data I expected to have
//App_Table is a subclass of NSManagedObject, it's my model class and get its data from coredata file
[self addMoreView];//Call this method will create a new subview and add it as subview to the UIScrollView, it will also update the page control, update the content size property, etc.

AppTableView *_appTableView = (AppTableView *) [[self.scrollView subviews] lastObject];//Remember addMoreView method create a new instance of AppTableView and add it as subview for the UIScrollView property, then I get that subview to fill it with data here

_appTableView.txtADDRESS.text = _appTable.address;//Fill in the form, no need to write all the form fields code because it's the same way.

// Scroll To First Page...
self.pageControl.currentPage = 0;
[self.scrollView scrollRectToVisible:CGRectMake(0, 0, viewWidth, viewHeight) animated:YES];

self.scrollView.contentSize = CGSizeMake(viewWidth*noOfItems, viewHeight);//Set the content size to the sum of subviews width, I also debugged that to check it's correct
self.scrollView.delegate = self;

[super viewDidLoad];
}

好的,所以当我有三个 subview 时, ScrollView 将加载三个 subview 的宽度,如上面的行所示:

self.scrollView.contentSize = CGSizeMake(viewWidth*noOfItems, viewHeight);//Set the content size to the sum of subviews width, I also debugged that to check it's correct

我可以滚动到 3 步(这是 subview 的数量)并且 UIPageControl 也设置为三个点,但只有一个 subview 可见,仅我能看到的最新一个,另外两个 subview 消失了, ScrollView 为它们计算了内容大小,但它们不可见。有什么想法吗 ?谢谢。

编辑:

值得注意的是,我第一次编辑 View 时,一切正常,当我处理 3 个 subview 时,它们都是可见的,但是当我转到另一个 View 并返回到这个 View 时,只有最后一个 subview 可见。

此外,我正在开发一个带有 Split View的 iPad 项目。

编辑:这是为 UIScrollView

绘制新 subview 的方法代码
 -(IBAction) addMoreView
{

NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"AppTableView" owner:self options:nil];

AppTableView *aView = [arr objectAtIndex:0];
[aView setFrame:CGRectMake(startX, 0, aView.bounds.size.width, aView.bounds.size.height)];
[self.scrollView addSubview:aView];
self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width+aView.frame.size.width
, self.scrollView.contentSize.height);


startX = startX + aView.frame.size.width;//Update the X position, first 0, then 600, 1200, and so on.
[self.scrollView scrollRectToVisible:aView.frame animated:YES];
NSLog(@"%f",self.scrollView.contentSize.width);
NSLog(@"%f",self.scrollView.contentSize.height);

}

假设我有 3 个 subview ,上面的方法将被调用 3 次,因为它被放在 for 循环中。所以这是上述方法的 NSLogs 的结果:

NSLog(@"%f",self.scrollView.contentSize.width);
NSLog(@"%f",self.scrollView.contentSize.height);

First iteration:

600.000000

0.000000

Second iteration:

1200.000000

0.000000

Third iteration:

1800.000000

0.000000

编辑:

rob mayoff 建议的命令让我明白了为什么会这样:

   |    |    |    | <AppTableView: 0x1eef4700; frame = (0 18; 600 430); autoresize = LM+RM+TM+BM; tag = 990; layer = <CALayer: 0x1eef4790>>

| | | | <AppTableView: 0x1ee3f380; frame = (0 18; 600 430); autoresize = LM+RM+TM+BM; tag = 991; layer = <CALayer: 0x1ee3f410>>

| | | | <AppTableView: 0x1ee56910; frame = (0 18; 600 430); autoresize = LM+RM+TM+BM; tag = 992; layer = <CALayer: 0x1ee3f410>>

所有三个 subview 都绘制在同一帧中,因此它们彼此重叠,但是当我在运行时使用断点调试时,x 正在改变,第一次是 0,然后是 600,然后是 1200这让我认为它绘制正确。如何解决这个问题,尤其是 x 值正确递增,那么问题是什么,为什么它们仍然在相同的 x 坐标上绘制?

最佳答案

首先,您应该将 [super viewDidLoad] 调用放在 - (void)viewDidLoad 方法的顶部,而不是底部。

鉴于您实际看到的内容和期望看到的内容不够清楚,提供您的问题的最小工作示例作为可下载项目将有助于我们为您提供帮助。如果您只是尝试在不与托管对象交互但使用一些静态提供的数据的单独 View Controller 中重现它,其他人将能够自己重现并调试它。或者您也可以在此过程中自行解决。

关于ios - ScrollView 仅显示最后添加的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16158170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com