2012-03-16 66 views
0

有什麼辦法可以將UITableView節頭視圖的寬度設置爲小於UITableView的整個寬度?現在,我設置tableView:viewForHeaderInSection:返回的視圖的寬度,標題視圖被拉伸到UITableView的寬度。如何設置UITableView標題視圖的寬度?

這裏是我的代碼:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIView *label = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 50, 50)]; 
    label.backgroundColor = [UIColor yellowColor]; 
    label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin; 
    return label; 
} 

回答

2

試試這個,如果我明白你的問題這必將爲你工作

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 60)]; 
    headerView.backgroundColor = [UIColor clearColor]; 

    UIView *label = [[UIView alloc] initWithFrame: CGRectMake(0,0, 50, 50)]; 
    label.backgroundColor = [UIColor yellowColor]; 
    label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin; 

    [headerView addSubview:label]; 
    return headerView; 
} 
+1

感謝兩位響應者。我點擊Bala的答案,因爲他的方法解決了我的問題。但嚴格來說,返回的headerView仍然在tableview中變得越來越緊張,如果將其背景顏色更改爲紅色,則會變得明顯。我仍然很好奇爲什麼發生這種情況,但至少我現在可以對我的應用程序進行最後的修改。 – 2012-03-17 16:33:43

0

簡單的解決方案:返回一個UIView其中包含什麼,但你所需的標題視圖,同時讓headerView的UIViewAutoresizing產權靈活右頁邊距,因此它不能擴展與它的superview(或者靈活的左邊界,或兩者,取決於你想要的東西;))。

+0

我試過了。它沒有工作。 – 2012-03-16 22:53:39

+0

你能發表一些代碼嗎?您必須提出錯誤,我所描述的解決方案無法失敗。 – Ahti 2012-03-16 23:22:37

2
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 

{ 
    int aSection = [[self.sectionsToDisplay objectAtIndex:section] integerValue]; 

    if([[self.groupHeadings objectAtIndex:aSection] isEqualToString:@""]) 

     return nil; 

    return [self.groupHeadings objectAtIndex:aSection]; 

}