我的UIScrollView
子视图出现了奇怪的行为,这个想法是以编程方式创建一个UIView
带有自定义 nib 文件的实例,在我的情况下这是一个表单,用模型类中的数据填充该表单,并将其添加为我的UIScrollView
. 问题是当我处理多个子视图时,UIScrollView
只保留最新的子视图,所以如果我创建了三个子视图,滚动视图将只显示第三个(最新的)子视图。虽然页面控件设置为子视图的核心数量(三个)。
该项目太长了,所以我将尝试用相关代码简要解释我的问题:
- (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];
}
好的,所以当我有三个子视图时,滚动视图将加载三个子视图的宽度,如上面的行计算:
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 次移动(这是子视图的数量)并且UIPageControl
也设置为三个点,但只有一个子视图可见,只有我能看到的最新一个,其他两个子视图消失了,滚动视图计算了内容它们的大小,但它们不可见。有什么想法吗 ?谢谢。
编辑:
值得注意的是,我第一次编辑视图时,一切正常,当我处理 3 个子视图时,它们都是可见的,但是当我转到另一个视图并回到这个视图时,只有最后一个子视图是可见的。
此外,我正在开发一个带有拆分视图的 iPad 项目。
编辑:
这是为UIScrollView
-(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 个子视图,上面的方法将被调用 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>>
所有三个子视图都绘制在同一个框架中,因此它们彼此上方,但是当我在运行时使用断点进行调试时,它们x
正在发生变化,第一次是 0,然后是 600,然后是 1200,这让我认为它绘制正确。如何解决这个问题,尤其是 x 值正在正确递增,那么问题是什么,为什么他们仍然在相同的 x 坐标上绘图?