簡體   English   中英

在分組表中設置標題時出現問題

[英]Problems setting view for header in a grouped table

我正在嘗試自定義分組表中各節的標題。 我為表的第一部分設置的視圖看起來不錯,但是后續部分的標題看起來像是在頂部和底部(在此屏幕截圖中僅顯示在頂部):

iPhone_groupedTable_header

我一直在為frame.size.origin嘗試不同的X和Y值,但是看起來還是一樣。 這是我的代碼:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
   if (section == 1) {
          UIView *wrapper = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 70)];
          [wrapper setBackgroundColor:[UIColor clearColor]];

          UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, self.tableView.frame.size.width, 20)];
          label.text = NSLocalizedString(@"Section 2 Header", @"");
          [label setFont:[UIFont boldSystemFontOfSize:15]];
          [label setBackgroundColor:[UIColor clearColor]];

          [wrapper addSubview:label];

          return wrapper;
   }

   else
          return nil;
}

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

我對所有節標題都進行了相同的操作,但是只有第一個正確顯示了,我該怎么做? 關於此問題,是否可以動態知道UILabel的高度,一旦知道它的文本和字體大小,還是應該始終“猜測”設置其框架大小? heightForHeaderInSection:方法中設置的標頭的視圖高度相同。

謝謝!

您未在此代碼中為第1節以外的其他節設置標題的高度,則應刪除if(section == 1)條件並為每個節提供相同的高度,然后檢查

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

謝謝,米塔爾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM