先介绍一下背景和环境:xcode7.2,用storyboard开发
问题:我在一个界面直接拖拽添加了一个tableview,我想给这个tableview添加一个表头,直接在控件上我不知道怎么添加,然后用代码写,可是写了添加不进去。看不到,调试的时候代码有执行,但是没看到。
代码段:
self.ViewTableHead.frame = CGRectMake(0
, 0
, self.oyitable.frame.size.width
, view_height_table_product);
self.ViewTableHead.backgroundColor = [UIColor blackColor];
UIButton *btn1
= [[UIButton alloc] initWithFrame:CGRectMake(0
, 0
, self.ViewTableHead.frame.size.width / 2
, self.ViewTableHead.frame.size.height)];
[btn1 setTitle:@"分类"
forState:(UIControlStateNormal)];
btn1.backgroundColor = [UIColor whiteColor];
btn1.tintColor = [UIColor blackColor];
UIButton *btn2
= [[UIButton alloc] initWithFrame:CGRectMake(0
, 0
, self.ViewTableHead.frame.size.width / 2 + 1
, self.ViewTableHead.frame.size.height)];
[btn2 setTitle:@"排序"
forState:(UIControlStateNormal)];
btn2.backgroundColor = [UIColor whiteColor];
btn2.tintColor = [UIColor blackColor];
[self.ViewTableHead addSubview:btn1];
[self.ViewTableHead addSubview:btn2];
self.oyitable.tableHeaderView = self.ViewTableHead;
楼主,这里提供两种方法
1.按照一楼所说,在viewForHeader代理方法里实现就可以了
2.建议还是用storyboard里实现headerView,这里附上一个链接,里边写的很清楚,
链接描述
希望能帮到你~