2013-05-14 123 views
0

我有一个使用pagecontrol的UIScrollView。我将代码中的表格添加到滚动视图中。 tableviews的每边应该有20px的填充。所以我做了以下。UITableview未正确放置在uiscrollview中

CGRect frame = self.scrollView.bounds; 
     frame.size.width = 280.0f; 
     frame.size.height = self.scrollView.bounds.size.height; 

     frame.origin.x = (320 * page)+20; 
     NSLog(@"orgin x is %d",(320 * page)+20); 
     NSLog(@"orgin x is %f",frame.origin.x); 
     frame.origin.y = 0; 


     UITableView *tableStage = [[UITableView alloc]initWithFrame:frame]; 
     tableStage.backgroundColor = [UIColor colorWithRed:(250/255.0) green:(248/255.0) blue:(247/255.0) alpha:100]; 
     tableStage.delegate = self; 
     tableStage.dataSource = self; 
     tableStage.tag = page; 

     tableStage.contentMode = UIViewContentModeScaleAspectFit; 
     [self.scrollView addSubview:tableStage]; 
     [self.pageViews replaceObjectAtIndex:page withObject:tableStage]; 

但我得到以下结果。 enter image description here

就像你可以看到下一个tableview总是移动更多的东西到左边。任何想法如何我可以解决这个问题?

+0

是什么[self.pageViews replaceObjectAtIndex:页withObject:tableStage]吗?用新表(tableStage)替换当前表? – Amar 2013-05-14 09:40:41

回答

1

试试这个

CGRect frame = self.scrollView.bounds; 
float offset=20; 
frame.size.width = frame.size.width-(2*offset); 
frame.origin.x = (320 * page)+offset; 
frame.origin.y = 0; 
UITableView *tableStage = [[UITableView alloc]initWithFrame:frame]; 

,不需此行

tableStage.contentMode = UIViewContentModeScaleAspectFit; 
+0

它仍在向左移动.. – Steaphann 2013-05-14 09:46:35

+0

仍然向左移动意味着什么? – 2013-05-14 09:49:24

+0

是的,但我现在的问题。 scrollview的宽度是326而不是320.感谢您的帮助! – Steaphann 2013-05-14 09:50:16

0
CGRect frame = self.scrollView.bounds; 
frame.size.width = 280.0f; 
frame.size.height = self.scrollView.bounds.size.height; 
frame.origin.x = (self.scrollView.bounds.size.width * page)+(self.scrollView.bounds.size.width - (frame.size.width/2)); 
frame.origin.y = 0; 
0

如果您正确设置了内容大小以及每个新表格视图的位置,它将完美地工作。也启用了分页。