2014-09-22 67 views
-6

我想在iOS應用程序中爲我的sectionHeaderView設置兩個不同的高度。我希望第一個sectionHeaderView是一個高度,第二個(所有其他)是另一個高度。截面的兩種不同高度HeaderView

注意:這不是表頭視圖,而是sectionHeaderView

- (CGFloat)tableView:(UITableView *)tableView heightForViewForHeaderInSection:  (NSIndexPath *)indexPath 
    { 
    if(indexPath.section == 0) 
    { 
     return 300; 
    } 
    return UITableViewAutomaticDimension; 

    else { 
    return 50; 

    } 
} 

謝謝。

+0

刪除回報UITableViewAutomaticDimension並嘗試 – NANNAV 2014-09-22 08:05:00

+0

這樣的代碼不能編譯(語法錯誤)。返回後還有其他聲明。 – Szu 2014-09-22 08:05:46

+0

說真的,這更像是一個'if-else'問題。與iOS無關。看看你的代碼,這是錯誤的。首先,你的方法不是'UITableViewDelegate'方法。也許這是故意的,而你以某種方式使用它。在那種情況下,它很好。該表不會調用此方法**永遠**。你的代碼在語義上**不正確,如果不是語法上的話。您在**和**其他**塊之間返回'UITableViewAutomaticDimension'。當**如果**計算結果爲真,它將返回300.否則,它將返回'UITableViewAutomaticDimension'。您的** else **案件將永遠不會被調用。 – n00bProgrammer 2014-09-22 08:24:43

回答

1

你的代碼是錯誤試試這個:

- (CGFloat)tableView:(UITableView *)tableView heightForViewForHeaderInSection:  (NSIndexPath *)indexPath 
    { 
     if(indexPath.section == 0) // First section 
     { 
      return 300; 
     } else if(indexPath.section == 1) { // Second 
      return 50; 
     } 
     else { // Any other section 
      return 40.0; // Default 
     } 
    } 
+0

這似乎應該工作,但沒有做任何改變高度。我有一個sectionHeadern.nib。那是壓倒一切的高度嗎? – user1155141 2014-09-22 21:01:32

0

小例子給你。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
    { 
     if (section == First) 
     { 
      return 70; 
     } 

     return 45; 
    }