【问题标题】:Table View Header autoresize issues表视图标题自动调整大小问题
【发布时间】:2014-11-05 19:14:18
【问题描述】:

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

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

【问题讨论】:

  • 是的,我正在尝试将自动调整大小设置为标题视图,因为它不像其他单元格那样自动调整大小

标签: ios ipad ios7 autoresize autoresizingmask


【解决方案1】:

我能够通过在我的 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;


}

【讨论】:

    猜你喜欢
    • 2017-05-14
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 2015-08-30
    • 1970-01-01
    相关资源
    最近更新 更多