popover中的表视图不滚动

时间:2013-01-12 10:49:26

标签: ios uitableview ios5 ios6 uipopovercontroller

我有下面的代码来显示带有tableview的popover,它完美无缺。

tableview显示13个数字,我可以滚动并看到相同的但是当我在模拟器上发布鼠标触摸时它会回到顶部。

它不会停留在显示的行,但会将我带回第1到第10行。

滚动后如何让它保持在位置。或者以下代码中的任何修复。

先谢谢。

- (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];

    }

我猜我需要更改以下值:

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

但是当我向下滚动时它会一直到达顶行。它不会停留在滚动到的同一行。

2 个答案:

答案 0 :(得分:7)

正确使用自动调整!

Popover - 内容大小

popoverView - 其初始大小应与popover的内容大小和用法相同 UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight

tblViewMenu - 其初始大小应与其祖先(popoverView)的大小相同,再次使用UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight

你的桌子不滚动,因为它太大了,不需要滚动。只有popover会剪掉它。

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;

答案 1 :(得分:0)

尝试设置tableView的ContentSize,如下所示:

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

另外,我更喜欢使tableView的大小动态化。取决于你的tableView数据源数组。

<强>更新

要使tableView动态化,请使用:

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

如果你有一个像其他单元格一样高的标题视图,只需在[数字计数]中加1,就像这样:

[tblViewMenu setContentSize:CGSizeMake(320, (([numbers count] + 1) * 32))];
相关问题