2013-08-07 23 views
2

在我的編碼中,表視圖將顯示,但是當我滾動表視圖時,它將完全滾動。我只滾動了表格視圖單元格。請任何人幫助我。這是我的代碼。如何添加TableView以UIScrollView的兩個部分作爲子視圖

[super viewDidLoad]; 
scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,460)]; 
scrollview.showsVerticalScrollIndicator=NO; 
scrollview.scrollEnabled=YES; 
scrollview.userInteractionEnabled=YES; 
[self.view addSubview:scrollview]; 
scrollview.contentSize = CGSizeMake(320,900); 
[scrollview addSubview:recipePhoto]; 
[scrollview addSubview:detailLabel]; 
[scrollview addSubview:prizeLabel]; 
[scrollview addSubview:discountLabel]; 
[scrollview addSubview:saveLabel]; 
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 480, 600, 400) style:UITableViewStylePlain]; 
[tableView setAutoresizesSubviews:YES]; 
[tableView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; 
tableView.scrollEnabled = NO; 
tableView.backgroundColor=[UIColor clearColor]; 
[tableView setDataSource:self]; 
[tableView setDelegate:self]; 
[scrollview addSubview:tableView]; 
[tableView reloadData]; 
+0

您能更清楚嗎?什麼是問題 – vamsi575kg

+0

你爲什麼使用UITableViewStylePlain。如果你需要兩節,那麼使用UITableViewStyleGrouped而不是UITableViewStylePlain。 –

+0

這似乎是這個問題的重複: http://stackoverflow.com/questions/3422915/scrolling-a-uitableview-inside-a-uiscrollview –

回答

2

我在您提供的代碼中看不到任何問題。您可能想檢查其他部分的意見或在這裏分享更多信息,以便我可以幫助您。

我在您的示例項目中添加了您的代碼,我可以滾動查看錶格視圖。以下是我使用的代碼(與您的代碼幾乎相同,對標籤進行了評論)

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,460)]; 
    scrollview.showsVerticalScrollIndicator=NO; 
    scrollview.scrollEnabled=YES; 
    scrollview.userInteractionEnabled=YES; 
    [self.view addSubview:scrollview]; 
    scrollview.backgroundColor = [UIColor grayColor]; 
    scrollview.contentSize = CGSizeMake(320,900); 
// [scrollview addSubview:recipePhoto]; 
// [scrollview addSubview:detailLabel]; 
// [scrollview addSubview:prizeLabel]; 
// [scrollview addSubview:discountLabel]; 
// [scrollview addSubview:saveLabel]; 
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 480, 600, 400) style:UITableViewStylePlain]; 
    [tableView setAutoresizesSubviews:YES]; 
    [tableView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; 
    tableView.scrollEnabled = NO; 
    tableView.backgroundColor=[UIColor clearColor]; 
    [tableView setDataSource:self]; 
    [tableView setDelegate:self]; 
    [scrollview addSubview:tableView]; 
    [tableView reloadData]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 1; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *cellIdentifier = @"CellIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 
    cell.textLabel.text = [NSString stringWithFormat:@"cell %d", indexPath.row]; 
    cell.backgroundColor = [UIColor blueColor]; 
    return cell; 
} 
0

這裏有兩個方式來完成該要求是:

只是設置與UITableViewcontentSize.height

UIScrollViewcontentSize只是你上面的代碼後添加以下代碼

tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.contentSize.height); 

float fscrview = tableView.frame.origin.y + tableView.contentSize.height + 20; 
yourScrollView.contentSize=CGSizeMake(320, fscrview); 

也設置scrollEnabled = NO;到的tableView,你在你的代碼

和當的TableView當時被滾動設置的UIScrollViewscrollEnabled = NO設置。

+0

但我有很多標籤在那裏。當我禁用scrollview意味着我不顯示剩餘的意見。 – user2656816

+0

好,然後按照我的上面的選項,並設置高度與其內容..設置每個表的高度與其內容和最後與最後一個表只是設置contentView的scrollView與最後一個表的高度.. –

相關問題