格式化UITableView标头

格式化UITableView标头,uitableview,Uitableview,我正在为我的UITableViewController创建自定义标题。我正在创建的标题显示在当前标题下,而不是替换它。如何更改此设置以替换默认设置,或者至少将新标题放置在顶部而不是底部 @interface RootViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> { ... } @implementation RootViewController - (

我正在为我的
UITableViewController
创建自定义标题。我正在创建的标题显示在当前标题下,而不是替换它。如何更改此设置以替换默认设置,或者至少将新标题放置在顶部而不是底部

@interface RootViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
... 
}



@implementation RootViewController

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 40.0;

    }

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{



  CGFloat height = [self tableView: tableView heightForHeaderInSection: section];

    UIView *header=[[UIView alloc]initWithFrame:CGRectMake(400,-10,300,height)];
    header.backgroundColor=[UIColor redColor];

    UILabel *headerLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,0,300,30)];
    headerLabel.backgroundColor=[UIColor redColor];
    headerLabel.shadowColor = [UIColor blackColor];
    headerLabel.shadowOffset = CGSizeMake(0,1);
    headerLabel.textColor = [UIColor whiteColor]; 
   headerLabel.text=@"Select";

    [header addSubview:headerLabel];

    [headerLabel release];
    return header;
}
@接口RootViewController:UITableViewController{
... 
}
@RootViewController的实现
-(CGFloat)表格视图:(UITableView*)表格视图头部高度部分:(NSInteger)部分{
回报率40.0;
}
-(UIView*)表格视图:(UITableView*)表格视图用于标题部分:(NSInteger)部分{
CGFloat height=[自表视图:表头的表视图高度section:section];
UIView*标题=[[UIView alloc]initWithFrame:CGRectMake(400,-10300,高度)];
header.backgroundColor=[UIColor redColor];
UILabel*headerLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,0300,30)];
headerLabel.backgroundColor=[UIColor-redColor];
headerLabel.shadowColor=[UIColor blackColor];
headerLabel.shadowOffset=CGSizeMake(0,1);
headerLabel.textColor=[UIColor whiteColor];
headerLabel.text=@“选择”;
[标题添加子视图:标题标签];
[头标释放];
返回头;
}
红色是我的习惯,不会取代蓝色标题。

它应该工作得很好。代码不包含错误。你确定它不工作吗?

蓝色标题是导航栏而不是tableview标题

选择您的导航控制器并将TopBar设置为none(无),如果您不需要此蓝色条;o)


我不明白你为什么把header的XPO写为400?哈!哦,天哪。谢谢你的帮助!它被设置为“无”,所以我想我需要对导航视图做些什么。