4

テーブルビューでポップオーバーを表示する以下のコードがあり、完全に機能します。

テーブルビューには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)]];

しかし、下にスクロールすると、一番上の行に来続けます。スクロールされているのと同じ行にとどまりません。

4

2 に答える 2

7

自動サイズ変更を正しく使用してください!

ポップオーバー - コンテンツ サイズあり

popoverView- 初期サイズは、ポップオーバーのコンテンツ サイズと同じにする必要があります。 UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight

tblViewMenu- 初期サイズは祖先 ( popoverView) のサイズと同じにする必要があります。UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight

テーブルが大きすぎてスクロールする必要がないため、テーブルはスクロールしていません。ポップオーバーだけがそれをクリッピングしています。

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;
于 2013-01-12T11:30:42.690 に答える
0

ContentSize次のように、tableView を設定してみてください。

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

また、tableView のサイズを動的にしたいと思います。tableView Datasource 配列に依存します。

アップデート

tableView を動的にするには、次を使用します。

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

そして、他のセルのように高さのあるヘッダー ビューがある場合は、[number count] に 1 を追加すると、次のようになります。

[tblViewMenu setContentSize:CGSizeMake(320, (([numbers count] + 1) * 32))];
于 2013-01-12T10:56:02.280 に答える