无法正确显示tableVIew中的headerVIew

时间:2016-11-10 03:58:01

标签: ios uitableview

我在使用此代码时创建了2个自定义tableView:

   UITableView *mainTableView=[[MainTableView alloc]init];
    [self.view addSubview:mainTableView];

我创建的headerView没有显示。

UITableView *mainTableView=[[MainTableView alloc]init];
self.view=mainTableView;

此代码显示正确的视图。我的headerView发生了什么?

WRONG RIGHT

UPDATE:MainTableView.m

+(instancetype)createMainView
{
    return [[self alloc]init];
}
-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    [self addmainTableView];
    [self addHeadImageViewAndPageControl];
    [self addTitleLabelAndNavigationButton];
    return self;
}
#pragma mark -add view to main view
-(void)addHeadImageViewAndPageControl
{
    UIScrollView *headImageView=[[UIScrollView alloc]init];
    headImageView.frame=CGRectMake(0, 0, yScreenWidth, yScreenHeight/3);
    headImageView.backgroundColor=[UIColor redColor];
    self.tableHeaderView=headImageView;
    //add pagecontroll
    UIPageControl *headPageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(-100, yScreenHeight/3-30, yScreenWidth/3, 25)];
    headPageControl.numberOfPages=5;
    headPageControl.pageIndicatorTintColor=[UIColor lightGrayColor];
    headPageControl.currentPageIndicatorTintColor=[UIColor whiteColor];
    headPageControl.currentPage=1;
    [self.tableHeaderView addSubview:headPageControl];
}

这是我的headerView。

1 个答案:

答案 0 :(得分:2)

您的代码问题不是Tableview的设置框架。在将tableView添加到self.view之前,请输入以下代码。

self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;