2011-02-27 98 views
0

我有使用自定義節標題視圖的UITableView控制器。當我的設備是肖像時,它顯示完美。而且,當我的設備再次呈現完美時。但是當在應用運行時期間改變方向時,它不會更新我的部分視圖。有什麼問題?iPhone - UITableViewController - 在縱向和橫向設置節標題

使用下面的代碼,我添加一個標籤和兩個按鈕到一個視圖,並返回該視圖在「viewForHeaderInSection」。在調試過程中,我發現該方法正在調用,但節標題視圖未更新。

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    NSDictionary* dict = [threadsArray objectAtIndex:section]; 

    UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)]; 
    headerView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.2]; 

    UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, self.view.frame.size.width - 98, 21)]; 
    newLabel.textColor = [UIColor blackColor]; 
    newLabel.font = [UIFont boldSystemFontOfSize:17]; 
    newLabel.text = [dict objectForKey:@"Name"]; 
    newLabel.backgroundColor = [UIColor clearColor]; 
    [headerView addSubview:newLabel]; 

    UIButton* addButton = [UIButton buttonWithType:UIButtonTypeContactAdd]; 
    addButton.frame = CGRectMake(self.view.frame.size.width - 88, 11, 29, 29); 
    NSLog(@"%f %f %f %f",addButton.frame.origin.x,addButton.frame.origin.y, addButton.frame.size.width,addButton.frame.size.height); 
    [addButton addTarget:self action:@selector(addThreadResponseClicked:) forControlEvents:UIControlEventTouchDown]; 
    [headerView addSubview:addButton]; 

    UIButton* expandCollapseButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [expandCollapseButton setImage:[UIImage imageNamed:@"Expand.png"] forState:UIControlStateNormal]; 
    expandCollapseButton.frame = CGRectMake(self.view.frame.size.width - 48, 11, 29, 29); 
    NSLog(@"%f %f %f %f",expandCollapseButton.frame.origin.x,expandCollapseButton.frame.origin.y, expandCollapseButton.frame.size.width,expandCollapseButton.frame.size.height); 
    [expandCollapseButton addTarget:self action:@selector(expandCollapseHeader:) forControlEvents:UIControlEventTouchDown]; 
    [headerView addSubview:expandCollapseButton]; 

    addButton.tag = expandCollapseButton.tag = section; 
    return [headerView autorelease]; 
} 

回答

1

嘗試設置觀點autoresizing masks適當:例如用於headerView;

[headerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; 

和Add按鈕和expandCollapseButton設置AutoresizingMask到:

UIViewAutoresizingMaskFlexibleLeftMargin 
+0

如果你看到我的代碼,當過取向的改變,我創造了新的headerView。即使我嘗試設置自動調整大小的蒙版。它沒有幫助我。 – Satyam 2011-03-01 17:31:12

+0

而不是引用視圖的框架使用界限,而是通常在添加子視圖時使用界限,因爲框架並不一定在您按照期望旋轉UI時切換寬度和高度(它有點隨意,我希望Apple修復) – 2011-03-01 18:01:14

+0

設置自動調整大小掩碼修復我的問題。謝謝 – Philip007 2012-11-08 00:50:48