2013-01-12 81 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

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