开发者

How to change the appearance of UITableViewController on rotate?

The task I'm currently trying to do with UITableViewController is to have one-column-cell-row in portrait mode, and two-column-cell-row in landscape mode. It's just for the viewing convenience (using available width space for viewing more rows-cells), so both column cells are of the same format. However, I'm not sure how to implement that.

So, the thought is to have my cell customization stuff开发者_开发技巧 in "cellForRowAtIndexPath" method and to check for the current screen mode. The question is do I have to set some flag at "shouldAutorotateToInterfaceOrientation" or there is some setting for that?

Second, would it be enough to call just table reload in "shouldAutorotateToInterfaceOrientation" in order to redraw cells of my table?

Also, I'm thinking about making different nib and designing my cell in IB. I guess it's another question, just wondering how does that would affect the solution.


You have to check the current orientation in cellForRowAtIndexPath and configure your cell properly. You can create 2 different cells with IB.

Also, you have to call [myTableView reloadData] in one of the callbacks for rotation events (shouldAutorotateToInterfaceOrientation or didRotateFromInterfaceOrientation). cellForRowAtIndexPath will be called each time you call [myTableView reloadData] (for all cells). Be sure you use different identifiers for reusing cells.

EDIT: This is how I would code this:

Add 2 IBOutlets to your .h file:

IBOutlet MyCustomCell1 * customCell1;
IBOutlet MyCustomCell2 * customCell2;

In Interface Builder, set the identifier property of each cell, maybe something like cellIdentifier1 and cellIdentifier2. Be sure that the file's owner in IB is your dataSource (the place where cellForRowAtIndexPath is implemented).

cellForRowAtIndexPath should look like this:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft 
    || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscaperight) 
    {
         //Landscape, lets use MyCustomCell2.
         NSString * cellIdentifier2 = @"cellIdentifier2";

         MyCustomCell2 * cell  = (MyCustomCell2 *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

         if (cell == nil) {
         //We have to initialize the cell, we're going to use IB
         [[NSBundle mainBundle] loadNibNamed:@"CustomCell2NibName" owner:self options:nil];
         //After this, customCell2 we defined in .h is initialized from IB 
         cell = customCell2;

         }
         //setup the cell, set text and everything.

         return cell;
    }

    else
    {
    //portrait case, the same as before but using CustomCell1
    NSString * cellIdentifier1 = @"cellIdentifier1";

         MyCustomCell1 * cell  = (MyCustomCell1 *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

         if (cell == nil) {
         //We have to initialize the cell, we're going to use IB
         [[NSBundle mainBundle] loadNibNamed:@"CustomCell1NibName" owner:self options:nil];
         //After this, customCell1 we defined in .h is initialized from IB 
         cell = customCell1;

         }
         //setup the cell, set text and everything.

         return cell;


     }

}


In your code for tableView:cellForRowAtIndexPath:, you can check the current orientation with:

if (self.interfaceOrientation == UIInterfaceOrientationPortrait ||
    self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    // use a portrait cell
} else {
    // use a landscape cell
}

Also, be sure to return YES from shouldAutorotateToInterfaceOrientation:. You should also reload the tableView after the rotation (in didRotateFromInterfaceOrientation:) with [tableView reloadData]; to make sure the correct cells are being used.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜