2011-04-27 87 views
0

我用tableHeaderView有一個簡單的uitableview。我有不止一節。是否可以使uitableheader通過第一個表格單元?

我設置了headerview的高度30.但我想在我的標題中使用40像素高度uiview(從0,0到320,30不透明,從30,10到320,40是透明的)。我在裏面放了一張照片。點(0x30)上有一個小圖標。它的高度= 10。 我只想在第一個tablecell上顯示我的headerview。

你會在下面看到我設置標題的高度爲30,但我在viewForHeaderInSection中創建了40像素高度的視圖。

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 


    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)]; 
    sectionView.backgroundColor = [UIColor redColor]; 
    [sectionView autorelease]; 
    return sectionView; 

} 

是否有可能?

回答

0

您的sectionView將受到標題高度的限制。即使使用1000像素高的UIView,它也只會顯示在由表格單元格/標題的高度所定義的範圍內。

+0

那麼,這是不可能的? – fulberto100 2011-04-27 10:01:08

+0

迄今爲止,我的知識是不可能的,除非有人想出了一個沒有找到的扭曲解決方法。 – 2011-04-27 11:48:58

0

也許我有你的問題的答案。我不知道這是否是一個好的解決方案,但我認爲它沒關係。

我做了一個新的空xib文件,並在該文件中放置了一個UITableViewCell。我將UITableViewCell的高度更改爲54,並將UIView放入UITableViewCell中。視圖的框架是0,0,320,10。然後我將該文件的xib文件的所有者更改爲我的TableViewController。

@interface RootViewController : UITableViewController { 

UITableViewCell * firstCellInSection; 

} 

@property (nonatomic, retain) IBOutlet UITableViewCell * firstCellInSection; 
@end 

之後,我將第一個CellInSection Outlet與我的UITableViewCell連接到xib文件中。

然後,我改變了TableView中加載這個firstCell所有細胞

indexPath.row == 0 

這裏是代碼代碼:

@synthesize firstCellInSection; 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell * cell; 
if (indexPath.row == 0) { 
    if (indexPath.row == 0) { 
     static NSString *firstCellInSectionIdentifier = @"FirstCellInSectionIdentifier"; 
     cell = [tableView dequeueReusableCellWithIdentifier:firstCellInSectionIdentifier]; 
     if (cell == nil) { 
      [[NSBundle mainBundle] loadNibNamed:@"FirstCell" owner:self options:nil]; 
      cell = firstCellInSection; 
      self.firstCellInSection = nil; 
     } 
    } 

} 

else { 

    static NSString *CellIdentifier = @"Cell"; 

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

} 



// Configure the cell. 
return cell; 
} 

自定義行高:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

if (indexPath.row == 0) return 54; 

else return 44; 
} 

在結合你的代碼爲headerView它可能看起來像你想要的。

+0

我還沒有嘗試過你的解決方案。根據你的解決方案,第一個單元會比其他單元(10像素)更大? – fulberto100 2011-04-27 10:22:23

+0

是的單元格是10 px更高 – 2011-04-27 10:31:49

+0

我認爲會有一個問題,如果用戶向上滾動,第一個單元格消失,每個單元格將相同。 – fulberto100 2011-04-27 10:38:53

相關問題