开发者

Setting up a UITableView with page cells and autoresizes its cell by autorotation of device

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

When an UITableViewController is automatically rotating, its UITableV开发者_JAVA技巧iew 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?


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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜