1

为什么我的 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;
4

1 回答 1

-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;
于 2013-07-02T16:33:30.413 回答