2

In my coding the table view will display but when i am scrolling the table view, it will completely scrolled. i have scrolled only table view cell. please any one help me. here is my code.

[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];
Shekhar Gupta
  • 6,206
  • 3
  • 30
  • 50
  • can you be more clear. what is the issue – vamsi575kg Aug 07 '13 at 12:48
  • Why have you used UITableViewStylePlain. If you need two section then use UITableViewStyleGrouped not UITableViewStylePlain. – Nishant Tyagi Aug 07 '13 at 12:48
  • This seems to be a duplicate of this question: http://stackoverflow.com/questions/3422915/scrolling-a-uitableview-inside-a-uiscrollview – Vinod Vishwanath Aug 07 '13 at 12:52
  • the table view has displayed but if i click cell it look like blue colour, if i click another cell it look like blue colour, but it will not change selection all cell are selected at same time. – user2656816 Aug 07 '13 at 12:54

2 Answers2

2

I don't see any problem in the code provided by you. You might want to check other parts of views or share more info here so that I can help you.

I added your code in my sample project and i was able to scroll to see the table view. Below is the code I used (almost same as yours, commented the labels)

- (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;
}
Ashok
  • 6,224
  • 2
  • 37
  • 55
0

Here Two Way to complete that requirements are:

1. just set contentSize of UIScrollView related to UITableView contentSize.height

just add this code after your above code

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);

also set scrollEnabled = NO; to the tableView as you set in your code

and 2. When TableView is scrolled at that time set scrollEnabled = NO of UIScrollView.

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • but i have lot of labels is there. when i disabled scrollview means i am not show remaining views. – user2656816 Aug 07 '13 at 12:59
  • ok then follow my above option and set Height with its content .. set every table Height with its content and also at last with last table just set contentSize of scrollView with that last tables height.. – Paras Joshi Aug 07 '13 at 13:00