2013-01-12 102 views
4

我有下面的代码来显示与tableview弹出窗口,它的作品完美。弹出窗口中的表视图不滚动

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

但是,当我向下滚动,它使未来到顶行。它不会停留在同一行正在滚动到。

+0

change popoverContent.contentSizeForViewInPopover = CGSizeMake(320,680); – NANNAV

+0

但是,这只会放大视图,但如果有50行,那么上述修复不会工作。我只是希望它留在特定的行。我该怎么做?再次感谢。 –

+0

基本上在tableview中没有滚动。 –

回答

7

正确使用自动调整大小!

酥料饼 - 具有内容大小

popoverView - 它的初始大小应该一样酥料饼的内容大小和使用 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; 
+0

谢谢,即使这个解决方案的作品完美。 –

+0

请确保你了解那里发生了什么。在编程中学习某些东西的方法是理解为什么不起作用。 – Sulthan

+0

我曾尝试上述代码没有autoresize,并没有工作之前,你提供的代码它工作得很好。 –

0

尝试设置的tableView的ContentSize,就像这样:

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

另外,我想prefere做出的tableView动态的大小。取决于你的tableView数据源数组。

UPDATE

为了使的tableView动态,使用此:

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

如果你有一个具有像其他细胞HIGHT头视图,只需添加1〜[数计],这将是这样的:

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

如何使tableView的大小动态化? –

+0

但是,这只会放大视图,但如果有50行,那么上述修复不会工作。我只是想让它留在特定的行。我该怎么做?再次感谢。 –

+0

如何让桌面停留在它的位置? –