4

我使用以下代码设置 uitableview 部分的标题视图。问题是,在我滚动表格视图之前,背景颜色(mainLightColor_2)不会出现。如何在加载时显示背景颜色?

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 24)];
  headerView.backgroundColor = [Utility mainLightColor_2]; //mainLightColor is a blue color

  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, 200, 20)];
  label.backgroundColor = [UIColor clearColor];
  label.textColor = [Utility mainDarkColor];
  label.text = @"Past Activities";
  [headerView addSubview:label];

  return headerView;
}

滚动前: 滚动前

滚动后: 滚动后

4

2 回答 2

1

So I found a solution to this. However, I still don't understand why setting the background color of headerView doesn't work.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 24)];

  UIView *headerViewBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 24)];
  headerViewBackground.backgroundColor = [Utility mainLightColor_2];
  [headerView addSubview:headerViewBackground];

  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, 200, 20)];
  label.backgroundColor = [UIColor clearColor];
  label.textColor = [Utility mainDarkColor];
  label.text = @"Past Activities";
  [headerView addSubview:label];

  return headerView;
}
于 2013-05-19T09:45:01.600 回答
1

您还应该实现- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section方法。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 24;
}
于 2013-05-19T09:39:08.427 回答