ios9代码写的控件看不到

先介绍一下背景和环境: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;
阅读 4k
5 个回答

楼主,这里提供两种方法
1.按照一楼所说,在viewForHeader代理方法里实现就可以了
2.建议还是用storyboard里实现headerView,这里附上一个链接,里边写的很清楚,
链接描述
希望能帮到你~

试试这么写,UITableViewDelegate

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    // 这里返回所需要的view
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    // 这里返回headerView所对应的高度
}

如果你没有实现在ViewTableHead的getter方法的话,可能需要先给ViewTableHead赋值。

新手上路,请多包涵

xib试下?

已经解决了,谢谢大家!因为一开始不知道怎么拖拽控件来实现才决定用代码实现的,后来用代码实现一直都无法加进去,郁闷。昨天有试了试,发现可以了:直接将要作为tabelheaderView的view拖拽到table上,要作为table的subview,然后控件自己就会当做tabelheaderView来处理了。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题