2013-01-12 2 views
4

아래 코드를 사용하여 popover를 tableview와 함께 표시하면 완벽하게 작동합니다.popover의 테이블 뷰가 스크롤되지 않습니다.

tableview는 13 개의 숫자를 표시하며 스크롤하고 볼 수 있지만 시뮬레이터에서 마우스를 놓으면 맨 위로 돌아갑니다.

표시되는 행에 머물러 있지 않지만 1에서 10 번째 행으로 이동합니다.

스크롤 한 후 어떻게 위치에 유지합니까? 또는 아래 코드의 수정.

미리 감사드립니다.

UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 680)]; 


    UITableView *tblViewMenu = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320,680)]]; 

내가 그런

UITableView *tblViewMenu = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320,320)]]; 

UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];  

를 사용하지만 아래로 스크롤 할 때오고 유지하는 경우 :

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

    } 

은 내가 아래의 값을 변경해야 추측 상단 행에. 그것은 스크롤되는 동일한 행.

+0

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

+0

그러나 그것은 단지보기를 확대 할 것이지만 50 행이 있다면 위의 수정은 작동하지 않을 것입니다. 나는 그 행에 머물고 싶습니다. 어떻게하면됩니까? 다시 한번 감사드립니다. –

+0

기본적으로 테이블 뷰에는 스크롤이 없습니다. –

답변

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

, 내가있는 tableView의 동적 크기를 만들기 위해 prefere 것입니다. 귀하의 tableView 데이터 소스 배열에 따라 달라집니다.

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

을 그리고 당신은 다른 셀과 같은 높이가있는 헤더보기가있는 경우, 단지 [숫자 수에 1을 추가

UPDATE

있는 tableView 동적를 만들기 위해 이것을 사용 ] 이는 다음과 같습니다.

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

tableView의 크기를 동적으로 만드는 방법은 무엇입니까? –

+0

하지만 그것은 단지보기를 확대 할 것이지만 50 행이 있다면 위의 수정은 효과가 없습니다. 단지 그 행에 남기를 원합니다. 어떻게해야합니까? 다시 한번 감사드립니다. –

+0

테이블 뷰를 그대로 유지하려면 어떻게합니까? –

관련 문제