设置带有页面单元格的 UITableView 并通过设备自动旋转自动调整其单元格大小

发布于 2024-10-27 11:18:48 字数 354 浏览 1 评论 0原文

(解决方案注释,不是问题,我会立即回答)

UITableViewController 自动旋转时,其 UITableView 会自动调整大小,但其单元格不会调整大小,甚至错误旋转后定位。

尤其是在分页的情况下,情况更糟。您可以将 pagingEnabled 设置为 YES,并将单元格高度设置为 [UITableViewbounds].size.height 以呈现分页。然而,在 UITableView 自动旋转之后,一切都变得一团糟。

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

(SOLUTION NOTE, NOT A QUESTION, I'LL ANSWER IMMEDIATELY)

When an UITableViewController is automatically rotating, its UITableView is resized automatically, but its cells are not resized and even wrongly positioned after rotation.

Especially, in the case of paging, this is worse. You may set pagingEnabled to YES and height of cells to [UITableView bounds].size.height to present paging. However after the UITableView auto-rotated, all things are messed.

How can I make those cells resized by table view rotation automatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中楼上月下 2024-11-03 11:18:48

像这样创建一个 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];
}

Make a UITableViewController like this. Core concept is beginUpdate before rotation, and endUpdate after rotation. With only these, cell's auto-resizing will be done automatically. However, their positioning will not. So I added a manual scroll to specific position.

- (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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文