【问题标题】:uitableview header background not shown until scrolluitableview 标题背景直到滚动才显示
【发布时间】:2013-05-19 09:24:43
【问题描述】:

我使用以下代码来设置 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;
}

滚动前:

滚动后:

【问题讨论】:

  • 尝试设置label.opaque = NO;
  • 刚刚尝试了您的代码,它运行良好,可能问题出在 [Utility mainLightColor_2] 方法上,尝试将其替换为 [UIColor blueColor] 只是为了测试,看看会发生什么。
  • 你可以尝试发布实用程序类代码吗?
  • @MariamN。那也没用。
  • 您在哪个 iOS 版本上进行测试?你在使用故事板吗?

标签: ios uitableview


【解决方案1】:

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

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

【讨论】:

  • 我实现了那个方法。
【解决方案2】:

所以我找到了解决方案。但是,我还是不明白为什么设置headerView的背景颜色不起作用。

- (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;
}

【讨论】:

  • 谢谢!在 iOS 8 上遇到了类似的问题,其中插入标题在您滚动它之前不会显示其背景颜色,并且在它离开屏幕并重新打开之前不会显示其 alpha 值。这个解决方法可以解决它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-24
  • 1970-01-01
  • 1970-01-01
  • 2013-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多