Tabular view in popover does not scroll

I have the code below to display a popover using tableview and it works fine.

Tableview displays 13 numbers, and I can scroll and see the same, but when I release the mouse on the simulator, it returns to the top.

It does not stay in the line displaying it, but returns me to the 1st and 10th line.

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

Thanks at 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 think I need to change the following values:

 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 getting to the top line. It does not stay on the same line that scrolls to.

+4
source share
2 answers

Use auto-implementation correctly!

Popover - has a content size

popoverView - its initial size must match the size of the popover content and use UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight

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

Your table does not scroll, because it is so large that it does not need to scroll. Only a popover crop 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; 
+7
source

Try setting the ContentSize View table, for example:

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

Also, I would prefer to make the size of the tableView speaker. depend on your TableView dataset.

UPDATE

To make a dynamic table tableView, use the following command:

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

And if you have a headline that has a height similar to other cells, just add 1 to the number [number], it will be as follows:

 [tblViewMenu setContentSize:CGSizeMake(320, (([numbers count] + 1) * 32))]; 
0
source

All Articles