Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to load landscape and portrait xib files for uitableviewcells?

My portrait tableviewcell doesn't work too well in landscape, even with an autoresize mask so I rearranged things with a new xib file.

What's the best way to implement this new landscape? I got it working using a bunch of if statements checking the orientation, but that doesn't seem too elegant. Is there a better way?

like image 897
Billy Shih Avatar asked Feb 03 '26 01:02

Billy Shih


1 Answers

Create two UITableViewCell nib files for portrait and landscape..and one custom class(CustomCell.h and CustomCell.m ) with UITableViewCell as the base class which the two NIB's will inherit and implement the following with your customized cells..

-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {


 if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
            [[NSBundle mainBundle] loadNibNamed:@"customCell-Landscape" owner:self options:nil];

        }else
 {
            [[NSBundle mainBundle] loadNibNamed:@"customCell-Portrait" owner:self options:nil];

}

[tableView reloadData];

}

and then in the tableview datasource

..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @" identifier";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {
        if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
            [[NSBundle mainBundle] loadNibNamed:@"customCell-Landscape" owner:self options:nil];
        }else {
            [[NSBundle mainBundle] loadNibNamed:@"customCell-Portrait" owner:self options:nil];
        }

    }
like image 109
AppleDelegate Avatar answered Feb 04 '26 15:02

AppleDelegate



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!