使用页面单元设置UITableView并通过设备的自动旋转自动调整其单元格

Eon*_*nil 0 iphone paging uitableview uikit auto-rotation

(解决方案注意,不是问题,我立刻回答)

当a UITableViewController自动旋转时,它UITableView会自动调整大小,但其单元格不会调整大小,甚至在旋转后也会错误定位.

特别是,在寻呼的情况下,这更糟.您可以将单元格设置pagingEnabledYES和高度[UITableView bounds].size.height以显示分页.但是在UITableView自动旋转之后,所有的东西都搞砸了.

如何通过表视图自动调整这些单元格的大小?

Eon*_*nil 5

做个UITableViewController像这样.核心概念是beginUpdate在旋转之前和endUpdate旋转之后.只有这些,单元格的自动调整大小将自动完成.但是,他们的定位不会.所以我添加了一个手动滚动到特定位置.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [tableView bounds].size.height;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return  YES;
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [[self tableView] beginUpdates];  
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];  
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
    [[self tableView] endUpdates]; 

    //  We have to specify an position to scroll to explicitly and manually
    //  becase current page number is no determinable especially if user rotate device continually.
    NSIndexPath*    indexPath   =   [NSIndexPath indexPathForRow:0 inSection:0];

    [[self tableView] scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

- (void) viewDidLoad
{
    [super viewDidLoad];

    [[self tableView] setPagingEnabled:YES];
}
Run Code Online (Sandbox Code Playgroud)