popover中的表视图不滚动 [英] table view in popover is not scrolling
问题描述
我有下面的代码来显示带有tableview的popover,它完美地运行。
I have the code below to display popover with tableview and it works perfectly.
tableview显示13个数字,我可以滚动查看相同的但是当我在模拟器上发布鼠标触摸时它会回到顶部。
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.
它不会停留在显示的行,但会将我带回第1到第10行。
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.
提前致谢。
- (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)]];
如果我使用
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
然后
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 .
推荐答案
正确使用自动调整!
Popover - 具有内容大小
Popover - has content size
popoverView
- 其初始大小应与popover的内容大小相同并使用
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
popoverView
- its initial size should be the same as popover's content size and use
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
tblViewMenu
- 其初始大小应与其大小相同祖先( popoverView
)再次使用 UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
tblViewMenu
- its initial size should be the same as the size of its ancestor (popoverView
), again, use UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
您的表格不滚动,因为它太大而不需要滚动。只有popover会剪切它。
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;
这篇关于popover中的表视图不滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!