2013-07-02 28 views
1

爲什麼我的UIView(下面創建)不出現在我嘗試添加到的UITableViewHeader中?UIView在UITableView標題中不可見

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; 
+0

你將你的UIView添加到ViewController的主視圖中? – ApolloSoftware

+1

[self.view addSubview:view];在最後... – lakesh

+1

@lakesh看看發佈代碼的最後一行。 – rmaddy

回答

-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 
} 

-As一個側面說明,如果你在使用標籤這裏設置爲標題的文本計劃,我建議尋找到下面的方法:

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

閱讀原始代碼。問題是關於表格的標題視圖,而不是節標題。 – rmaddy

+0

節頭或表頭,無論如何,這已停止在iOS7上工作...有誰知道任何解決方案? – mindbomb