繁体   English   中英

UIView在UITableView标头中不可见

[英]UIView not visible in UITableView Header

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM