表视图标头自动调整问题

时间:2014-09-11 18:02:10

标签: ios ipad ios7 autoresize autoresizingmask

我的视图控制器中有一个tableview,设置为在xibs中自动调整大小,并且在potrait和横向方向上都能正常工作。但是,当我通过代码添加自定义标题视图时,它与其他单元格无法正确对齐,我的标题具有自定义单元格,该单元格在表格中显示为其子视图。我尝试了很多autoresizingmask属性,但我不能正确。任何帮助将不胜感激。下面是lanscape和potrait方向的图像以及我的viewcontroller.m代码

#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

以下是此示例项目的链接 http://www.mediafire.com/download/53m6riblk2dkjk7/TableHeaderTest.zip

1 个答案:

答案 0 :(得分:0)

我能够通过在tableviewcell类中设置layoutsubviews时定义帧大小来解决这个问题

- (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;


}