Ios 表视图标题自动调整大小问题
我的视图控制器中有一个tableview,它在xibs中设置为自动调整大小,在potrait和横向方向上都可以正常工作。然而,当我通过代码添加自定义标题视图时,它并没有与其他单元格正确对齐,我的标题有一个自定义单元格,该单元格作为其子视图显示在表中。我已经尝试了很多自动重设网格属性,但我就是做不好。任何帮助都将不胜感激。下面是lanscape和potrait方向中的图像以及我的viewcontroller.m代码Ios 表视图标题自动调整大小问题,ios,ipad,ios7,autoresize,autoresizingmask,Ios,Ipad,Ios7,Autoresize,Autoresizingmask,我的视图控制器中有一个tableview,它在xibs中设置为自动调整大小,在potrait和横向方向上都可以正常工作。然而,当我通过代码添加自定义标题视图时,它并没有与其他单元格正确对齐,我的标题有一个自定义单元格,该单元格作为其子视图显示在表中。我已经尝试了很多自动重设网格属性,但我就是做不好。任何帮助都将不胜感激。下面是lanscape和potrait方向中的图像以及我的viewcontroller.m代码 #import "ViewController.h" @interface Vi
#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
这里有一个指向这个示例项目的链接
我可以通过在tableviewcell类中设置LayoutSubView时定义帧大小来解决此问题
- (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;
}
是的,我正试图将autoresize设置为header视图,因为它不像其他单元格那样自动调整大小