怎么为tableview添加addheaderview

下次自动登录
现在的位置:
& 综合 & 正文
如何让 UITableView 的 headerView跟随 cell一起滚动
在我们利用 UITableView 展示我们的内容的时候,我需要在顶部放一个不同于一般的cell的 界面,这个界面比较独特。
1。 所以我就把它 作为一个section的 headerView。
也就是在函数:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
里面返回 这个UIView。
但是,由于这个UIView占的空间很大,基本占用整个屏幕的高度,而滚动tableView的时候,只滚动cell的内容,而这个section的
headerView却不跟着滚动。
后面,我想出了方法2。
设置 tableView的 style为
UITableViewStyleGrouped,然后让
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
这样确实可以让 headerView ,在滚动tableView的时候,跟随着cell的内容一起滚动。但是,我发现,下面的cell都被加上了边框,
而且cell的水平显示范围变窄了。
所以尝试了方法3。
将UIView设置为
整个tableView的headerView,而不是 section 0的headerView
self.tableView.tableHeaderView=
这样,就可以完美的满足 headerView跟随cell的内容一起滚动的要求拉。
结论:设置 UIView为
tableView的tableHeaderView即可实现 headerView跟随tableView一起滚动的效果。
&&&&推荐文章:
【上篇】【下篇】在Storyboard中为UITableView添加Header和Footer - 推酷
在Storyboard中为UITableView添加Header和Footer
我在这里所说的Header和Footer并不是 sectionHeader 和 sectionFooter ,而是指 UITableView 的 tableHeaderView 和 tableFooterView ,这两个可以跟随 tableView 滑动的头部和尾部.
使用代码添加:
首先需要用代码(或者使用xib)创建一个继承自 UIView 的 headerView 或者 footerView ,然后使用下列代码给 tableView 增加头部和尾部.
self.tableView.tableHeaderView = headerVself.tableView.tableHeaderView = footerV
但是这种方法相对来说比较麻烦,在Storyboard可以很轻松的完成这个任务.
在Storyboard中添加:
添加的方法非常简单,直接拖就OK了,并且能直接在headerView和footerView中使用AutoLayout给子控件布局.
添加到 UITableViewCell 上面的就是 headerView ,下面的就是 footerView ,如果没有给 UITableView 添加Cell的话,拖到 UITableView 里面默认是 headerView ,所以如果需要添加尾部,确保有至少一个Cell就OK了.
修改tableHeaderView和tableFooterView的高度:
自适应高度的Header和Footer
如果希望 tableHeaderView 和 tableFooterView 根据内容自适应高度,则需要用到 -systemLayoutSizeFittingSize: 这个方法来实现.
实现自适应高度的前提,约束要完整,要保证View内部上下左右所有方向都有约束支撑.例如下图的一个例子:
写好了约束后,在-viewDidLayoutSubviews中添加如下代码:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
//利用systemLayoutSizeFittingSize:计算出真实高度
CGFloat height = [self.tableView.tableHeaderView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].
CGRect headerFrame = self.tableView.tableHeaderView.
headerFrame.size.height =
//修改tableHeaderView的frame
self.tableView.tableHeaderView.frame = headerF}
这样就能实现高度自适应啦~~~
给Label增加大段文本,并且修改 numberOfLines 为0,运行后的效果:
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致1375人阅读
& &&if (UI_USER_INTERFACE_IDIOM()!=UIUserInterfaceIdiomPad) {
& & & & UILabel*headlable=[[UILabel alloc] init];
& & & & headlable.textColor=[UIColor blackColor];
& & & & headlable.text=@&「ユーザー情報」&;
& & & & headlable.font=[UIFont boldSystemFontOfSize:16];
& & & & headlable.frame=CGRectMake(0,
0, 150, 30);
& & & & headlable.textAlignment=NSTextAlignmentL
& & & & UILabel*detailLabel=[[UILabel alloc] init];
& & & & NSString * title=@&※社内用メールアドレスを頂く理由としては、利用ユーザーをMR様に限定するためです。&;
& & & & CGSize size =[title sizeWithFont:[UIFont systemFontOfSize:11] constrainedToSize:CGSizeMake(self.view.frame.size.width,
30) lineBreakMode:NSLineBreakByCharWrapping];
& & & & detailLabel.numberOfLines=0;//不限制行数
& & & & detailLabel.textColor=[UIColor blackColor];
& & & & detailLabel.text=
& & & & detailLabel.font=[UIFont systemFontOfSize:11];
& & & & detailLabel.frame=CGRectMake(5,
30,[UIScreen mainScreen].bounds.size.width, size.height);
& & & & detailLabel.backgroundColor=[UIColor clearColor];
& & & & detailLabel.textAlignment=NSTextAlignmentL
& & & & UIView *footerView = [[UIView alloc]initWithFrame: CGRectMake(0,
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
self.tableView.frame.size.width,
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & footerView.backgroundColor = [UIColor clearColor];
& & & & [footerView addSubview:headlable];
& & & & [footerView addSubview:detailLabel];
& & & & self.tableView.tableHeaderView = footerV
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:97659次
积分:1699
积分:1699
排名:千里之外
原创:65篇
转载:49篇
评论:10条
(1)(1)(2)(4)(3)(2)(2)(1)(3)(11)(4)(2)(2)(1)(10)(2)(1)(3)(6)(1)(23)(27)(6)

我要回帖

更多关于 webview 添加header 的文章

 

随机推荐