2014-09-11 5 views
0

xview에서 autoresize하도록 설정된 뷰 컨트롤러에 tableview가 있으며 potrait와 landscape orientation에서 잘 작동합니다. 그러나 코드를 통해 사용자 정의 헤더보기를 추가 할 때 다른 셀과 제대로 정렬되지 않습니다. 내 헤더에는 테이블에 하위 뷰로 표시되는 사용자 정의 셀이 있습니다. 나는 많은 autoresizingmask 재산을 시도했다 그러나 나는 다만 기울인다 그것을 맞은 얻는다. 어떤 도움을 주시면 감사하겠습니다. 다음은 lanscape에와 potrait의 방향을 내 viewcontroller.m 코드 여기테이블 뷰 헤더 autoresize 문제

#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

+0

예. 다른 셀과 같이 자동 크기 조정이 아니므로 헤더보기에 자동 크기를 설정하려고합니다. – Gamerlegend

답변

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; 


}