代码之家  ›  专栏  ›  技术社区  ›  Viral Narshana

在uiTableView的标题中没有显示标题?

  •  1
  • Viral Narshana  · 技术社区  · 15 年前

    我正在使用此代码使用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;
    }
    

    请帮忙…

    2 回复  |  直到 13 年前
        1
  •  1
  •   sho    14 年前

    如果我没记错的话,一旦你还给我 viewForHeaderInSection 返回的值 titleForHeaderInSection 不再使用。在 标题插入视图 将uilabel作为子视图添加到 headerView 并将文本设置为 @"Contents"

        2
  •  0
  •   Dmytro Zarezenko    12 年前

    请将您的代码更改为this,并仅检查您必须实现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;
    }
    
    推荐文章