UIView在UITableView标头中不可见

时间:2013-07-02 16:24:28

标签: ios objective-c uitableview uiview

为什么我的UIView(下面创建的)不会显示在我尝试添加到的UITableViewHeader中?

UIView *view = [[UIView alloc] initWithFrame: CGRectMake ( 20, 20, 400, 400)];
searchBar = (UISearchBar *)self.tableView.tableHeaderView;
[searchBar.subviews objectAtIndex:0] removeFromSuperview];
searchBar.backgroundColor = [UIColor clearColor];
[view addSubview:searchBar];
self.tableView.tableHeaderView = nil;
view.backgroundColor = [UIColor redColor];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, self.view.frame.size.width, 60)];
label.shadowColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:.35];
label.shadowOffset = CGSizeMake(0, -1.0);
label.text = @"188 People";
label.textColor = [UIColor grayColor];
label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:12.0];
label.backgroundColor = [UIColor clearColor];
[view addSubview:label];

self.tableView.tableHeaderView = view;

1 个答案:

答案 0 :(得分:-1)

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame: CGRectMake ( 20, 20, 400, 400)];
    searchBar = (UISearchBar *)self.tableView.tableHeaderView;
    [searchBar.subviews objectAtIndex:0] removeFromSuperview];
    searchBar.backgroundColor = [UIColor clearColor];
    [view addSubview:searchBar];
    view.backgroundColor = [UIColor redColor];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, self.view.frame.size.width, 60)];
    label.shadowColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:.35];
    label.shadowOffset = CGSizeMake(0, -1.0);
    label.text = @"188 People";
    label.textColor = [UIColor grayColor];
    label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:12.0];
    label.backgroundColor = [UIColor clearColor];
    [view addSubview:label];

    return view;//Please note the header will look the same for each section. You can add a switch or if/else to augment this
}

- 作为旁注,如果您打算在此处使用标签设置标题的文本,我建议您查看以下方法:

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;