4
後,我要建立一個自定義tableViewHeader在應該顯示至少兩個標籤,即一個projectName和projectDescription。這兩個標籤的內容有所不同(即按照字符串長度)。如何自定義更改viewForHeaderInSection寬度旋轉
我設法得到一個工作版本的默認設備方向(肖像),但如果用戶旋轉設備橫向我的自定義headerView的寬度不調整,用戶將看到右側未使用的空白。
下面的代碼片段被用於:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
CGFloat wd = tableView.bounds.size.width;
CGFloat ht = tableView.bounds.size.height;
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0., 0., wd, ht)];
headerView.contentMode = UIViewContentModeScaleToFill;
// Add project name/description as labels to the headerView.
UILabel *projName = [[UILabel alloc] initWithFrame:CGRectMake(5., 5., wd, 20)];
UILabel *projDesc = [[UILabel alloc] initWithFrame:CGRectMake(5., 25., wd, 20)];
projName.text = @"Project: this project is about an interesting ..";
projDesc.text = @"Description: a very long description should be more readable when your device is in landscape mode!";
[headerView addSubview:projName];
[headerView addSubview:projDesc];
return headerView;
}
我注意到tableView:viewForHeaderInSection:
僅會被viewDidLoad
後調用,而不是設備方向更改後。
如果我的東西,如實施willRotateToInterfaceOrientation:
方法:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
[self.tableView reloadSections:nil withRowAnimation:UITableViewRowAnimationNone];
}
我無法弄清楚如何獲得reloadSections:
工作,迫使我customView的重新計算。