headerView跟随 cell一起滚动 及headerView 作为表头背景的实现
2013-12-01 14:59
483 查看
UITableView 的 表头背景 和 headerView跟随 cell一起滚动
这是我在做一个项目时遇到的问题:怎样像新浪微博或者QQ空间里的:“个人空间”那样,在表格最上方有个背景图片(把背景放在表头)?向下拖动时能显示更多的图片内容?向上拖时,[b]表格的[/b]UITableViewStyle不用改为group,headerView也能跟随一起滚动?
1.向下拖动时能显示更多的图片内容?
可以这样:设置UIEdgeInsetsMake(top, left, bottom, right);
,比如UIEdgeInsetsMake(-100,0,0,0)。效果是:表格的最上方插入一段高-100大小的空余,很明显-100就是表格显示时从表格内容高度100的地方开始显示,高度比100小的被隐藏了,只有向下拖动才能看到。(解决)
2.[b]向上拖时,表格的UITableViewStyle不用改为group,headerView也能跟随cell一起滚动?[/b]
这个很简单,但是花了哥哥不少时间。(主要是各种“百” 、各种“度”,但是都没满意的做法,可能有经验的早就知道了。不过没分享出来,至少我找了好久都没找到!!)
只要:设section==0时,cell的数量为0
OK了,问题解决了。
链接
http://blog.csdn.net/tangaowen/article/details/6452314
他的第三个方法是什么意思,我没弄懂。
有个不满意地方:新浪微博的背景能拉伸,该怎么搞还得研究研究,如果你有好方法
![](https://oscdn.geek-share.com/Uploads/Images/Content/201603/899229cfab2c02d614490485cabb781b.gif)
。
下面是代码:头文件定义一个UITableView的子类,图片自己搞
#import "ViewController.h"
@interfaceViewController ()
@end
@implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
returnself;
}
- (void)viewDidLoad
{
[superviewDidLoad];
self.view.backgroundColor=[UIColorgrayColor];
_table=[[UITableViewalloc]initWithFrame:CGRectMake(0,10,320,
480)style:UITableViewStylePlain];
_table.backgroundView=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"2.1.png"]];
_table.contentInset=UIEdgeInsetsMake(-100,0,0,
0);
_table.delegate=self;
_table.dataSource=self;
[self.viewaddSubview:_table];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0) {
return0;
}
return5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
staticNSString *CellIdentifier =@"Cell";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:CellIdentifier];
}
cell.textLabel.text=@"fdsafe";
cell.backgroundView=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"2.1.png"]];
return cell;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath{
return111;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section==0) {
return200;
}
return0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIImageView *img=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"backGround.png"]];
img.frame=CGRectMake(10,0,320,
400);
return img;
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
这是我在做一个项目时遇到的问题:怎样像新浪微博或者QQ空间里的:“个人空间”那样,在表格最上方有个背景图片(把背景放在表头)?向下拖动时能显示更多的图片内容?向上拖时,[b]表格的[/b]UITableViewStyle不用改为group,headerView也能跟随一起滚动?
1.向下拖动时能显示更多的图片内容?
可以这样:设置UIEdgeInsetsMake(top, left, bottom, right);
,比如UIEdgeInsetsMake(-100,0,0,0)。效果是:表格的最上方插入一段高-100大小的空余,很明显-100就是表格显示时从表格内容高度100的地方开始显示,高度比100小的被隐藏了,只有向下拖动才能看到。(解决)
2.[b]向上拖时,表格的UITableViewStyle不用改为group,headerView也能跟随cell一起滚动?[/b]
这个很简单,但是花了哥哥不少时间。(主要是各种“百” 、各种“度”,但是都没满意的做法,可能有经验的早就知道了。不过没分享出来,至少我找了好久都没找到!!)
只要:设section==0时,cell的数量为0
OK了,问题解决了。
链接
http://blog.csdn.net/tangaowen/article/details/6452314
他的第三个方法是什么意思,我没弄懂。
有个不满意地方:新浪微博的背景能拉伸,该怎么搞还得研究研究,如果你有好方法
![](https://oscdn.geek-share.com/Uploads/Images/Content/201603/899229cfab2c02d614490485cabb781b.gif)
。
下面是代码:头文件定义一个UITableView的子类,图片自己搞
#import "ViewController.h"
@interfaceViewController ()
@end
@implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
returnself;
}
- (void)viewDidLoad
{
[superviewDidLoad];
self.view.backgroundColor=[UIColorgrayColor];
_table=[[UITableViewalloc]initWithFrame:CGRectMake(0,10,320,
480)style:UITableViewStylePlain];
_table.backgroundView=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"2.1.png"]];
_table.contentInset=UIEdgeInsetsMake(-100,0,0,
0);
_table.delegate=self;
_table.dataSource=self;
[self.viewaddSubview:_table];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0) {
return0;
}
return5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
staticNSString *CellIdentifier =@"Cell";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:CellIdentifier];
}
cell.textLabel.text=@"fdsafe";
cell.backgroundView=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"2.1.png"]];
return cell;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath{
return111;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section==0) {
return200;
}
return0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIImageView *img=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"backGround.png"]];
img.frame=CGRectMake(10,0,320,
400);
return img;
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- Python核心编程--学习笔记--7--字典和集合
- 制作飞镖忍者(1) Cocos2d-x 3.0alpha0
- 第十四周-成绩处理
- 养老问题
- HTTP协议是无状态协议,Session,Cookie的解释
- [JAVA]《Java 核心技术》(四)多线程
- 自进程名找apk
- OC字符串常用函数
- java中绝对路径和相对路径的总结
- 每天进步一点点-------python之时钟
- POJ 1018 Communication System DP
- poj1003 Hangover
- linux Makefile
- 14--5--1 Fun而已
- 编程中遇到的bug调试问题
- Linxu基本学习之挂载
- 《Win32多线程程序设计》读后感
- QZone V8改版之“背景自适应”
- SVM求解之坐标上升算法(Coordinate Ascent)
- Android工程目录及其作用简介