【问题标题】:Title in header in UITableView not shown?UITableView 标题中的标题未显示?
【发布时间】:2010-09-22 13:24:27
【问题描述】:

我正在使用此代码通过 UITableView 设置标题和背景颜色,但未显示标题?

- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section
{
    return @"Contents";
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)] autorelease];
    [headerView setBackgroundColor:[UIColor brownColor]];
    [self.view sendSubviewToBack:headerView];

    return headerView;
}

请帮忙...

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    如果我没记错的话,一旦你为viewForHeaderInSection 返回了一些东西,titleForHeaderInSection 中返回的值就不再使用了。在viewForHeaderInSection 中添加一个UILabel 作为headerView 的子视图,并将文本设置为@"Contents"

    【讨论】:

      【解决方案2】:

      请将您的代码更改为此并检查您必须实现 viewForHeaderInSection 方法:

      - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
      {
      
          NSString *cellValue = @"";
          UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)] ;
          [headerView setBackgroundColor:[UIColor brownColor]];
      
          UILabel *lblContent = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, headerView.frame.size.width,  headerView.frame.size.height)];
          cellValue = [[arrSections objectAtIndex:0] valueForKey:@"CategoryName"];
          lblContent.text = cellValue;
          [headerView addSubview:lblContent];
          return headerView;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-18
        • 2018-11-07
        • 1970-01-01
        • 1970-01-01
        • 2021-06-15
        • 1970-01-01
        相关资源
        最近更新 更多