Search code examples
iosipadios7autoresizeautoresizingmask

Table View Header autoresize issues


I have a tableview in my view controller that is set to autoresize in the xibs and works fine on both potrait and landscape orientation. However when I added a custom header view through code it doesnt align properly with other cells, my header has the custom cell which is displayed on table as its subview. I have tried many autoresizingmask properties but I just cant get it right. Any help will be appreciated. Below are the images in lanscape and potrait orientation and my viewcontroller.m code

#import "ViewController.h"

@interface ViewController ()
{
    NSMutableArray *tempArray;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    tempArray=[NSMutableArray arrayWithObjects:@"Jack",@"Rose",@"Juliet", nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}



#pragma mark - UITableView Datasource
#pragma mark

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    return 60.0f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return tempArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CGFloat headerHeight = 40.0f;

    NSArray *nib    =   nil;

        nib    =   [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell_iPad" owner:self options:nil];

    CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
    UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, headerHeight)];
    [headerView setBackgroundColor:[UIColor clearColor]];

    MyCustomCell* mpView   =   [nib objectAtIndex:0];

    [mpView.testLabel setText:@"Name"];
    [mpView.testLabel2 setText:@"Age"];
    [mpView.testLabel3 setText:@"Date"];

    mpView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;


    [headerView addSubview:mpView];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
        return 60.0f;
}

#pragma mark - UITableView Delegate
#pragma mark

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

    if (pCell == nil)
    {
        NSArray *nib ;

            nib    =   [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell_iPad" owner:self options:nil];


        pCell           =   (MyCustomCell*)[nib objectAtIndex:0];

        pCell.backgroundColor = [UIColor clearColor];
    }



            [pCell.testLabel setText:[tempArray objectAtIndex:indexPath.row]];


    return pCell;
}


-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    [_myTable reloadData];
}


@end

ipad Potrait

ipad Landscape

Here is a link to this sample project http://www.mediafire.com/download/53m6riblk2dkjk7/TableHeaderTest.zip


Solution

  • I was able to solve this issue by defining the frame size at the time of setting layoutsubviews in my tableviewcell class

    - (void) layoutSubviews
    {
        [super layoutSubviews];
    
            CGRect contentViewFrame = self.contentView.frame;
            CGPoint viewCenter= self.contentView.center;
            contentViewFrame.size.width = self.frame.size.width;
            self.contentView.frame = contentViewFrame;
            self.contentView.center=viewCenter;
    
    
    }