5

I have the code below to display popover with tableview and it works perfectly.

The tableview displays 13 numbers and I can scroll and see the same but when I release mouse-touch on simulator it goes back to the top.

It does not stay at the row which its displaying but takes me back to 1st to 10th row .

How do I make it stay in the position after scrolling. Or any fix in the below code.

Thanks in Advance.

- (IBAction)btn:(id)sender {

    if([popoverController isPopoverVisible])
    {
        [popoverController dismissPopoverAnimated:YES];
        return;
    }
    //build our custom popover view
    UIViewController* popoverContent = [[UIViewController alloc]init];
    UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 680)];
    popoverView.backgroundColor = [UIColor blackColor];

    numbers = [[NSMutableArray alloc] initWithObjects:@"1",@"2", @"3",@"4",@"5", @"6",@"7", @"8", @"9",@"10",@"11", @"12",@"13",nil];


    UITableView *tblViewMenu = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 680)];
    tblViewMenu.delegate = self;
    tblViewMenu.dataSource = self;
    tblViewMenu.rowHeight = 32;

    [popoverView addSubview:tblViewMenu];
    popoverContent.view = popoverView;
    popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 320);
    popoverController = [[UIPopoverController alloc]
                              initWithContentViewController:popoverContent];

    [popoverController  presentPopoverFromRect:self.btn.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [numbers count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    }

    // Configure the cell...
    NSString *color = [numbers objectAtIndex:indexPath.row];
    cell.textLabel.text = color;

    return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        NSString *number = [numbers objectAtIndex:indexPath.row];

    NSLog(@"%@",number);

    [popoverController dismissPopoverAnimated:YES];

    }

I Guess I need to change the values below:

UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 680)];


    UITableView *tblViewMenu = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320,680)]];

If I use

UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];     

Then

  UITableView *tblViewMenu = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320,320)]];

But when I scroll down it keeps coming to the top row .It does not stay in the same row which is being scrolled to .

Jacob Wood
  • 467
  • 9
  • 26

2 Answers2

7

Use autoresizing correctly!

Popover - has content size

popoverView - its initial size should be the same as popover's content size and use UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight

tblViewMenu- its initial size should be the same as the size of its ancestor (popoverView), again, use UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight

Your table is not scrolling because it is so big that it doesn't needs scrolling. Only the popover is clipping it.

CGSize contentSize = CGSizeMake(320, 320);

UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, contentSize.width, contentSize.height)];
popoverView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

UITableView *tblViewMenu = [[UITableView alloc]initWithFrame:popoverView.bounds];
tblViewMenu.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

popoverContent.contentSizeForViewInPopover = contentSize;
Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • Please make sure you understand what's happening there. The way to learn something in programming is to understand why something doesn't work. – Sulthan Jan 12 '13 at 11:42
  • I had tried the above code without autoresize and that did not work before with the code you provided it worked perfectly fine. – Jacob Wood Jan 12 '13 at 11:45
  • I now understand the reason for the problem . Thanks. – Jacob Wood Jan 12 '13 at 11:45
  • I have a similar problem that I cannot seem to resolve - it is described here: http://stackoverflow.com/questions/20206599/uitableview-inside-popover-will-not-scroll. The difference is that my view and table view are created using NIB files, and the tableview is shown in the second view controller placed inside a popup, rather than the first. I tried setting the content size of the view and table view but that didn't work. I also don't understand the statement "Your table is not scrolling because it is so big that it doesn't needs scrolling. Only the popover is clipping it." – Marc Nov 26 '13 at 03:04
  • When a table gets big, that is exactly when it needs scrolling. A small table does not need scrolling because the entire table can be shown in one screen. So the statement above makes no sense to me. – Marc Nov 26 '13 at 03:05
0

Try set the ContentSize of the tableView, like this:

[tblViewMenu setContentSize:CGSizeMake(320,680)];

Also, I'd prefere to make the size of the tableView dynamic. depend on your tableView Datasource array.

UPDATE

To make the tableView dynamic, use this:

[tblViewMenu setContentSize:CGSizeMake(320, ([numbers count] * 32))];

And if you have a header view that has a hight like the other cells, just add 1 to [number count], that would be like this:

[tblViewMenu setContentSize:CGSizeMake(320, (([numbers count] + 1) * 32))];
Scar
  • 3,460
  • 3
  • 26
  • 51