headerView跟随 cell一起滚动 及headerView 作为表头背景的实现

来源:互联网 发布:黄金箱体指标源码 编辑:程序博客网 时间:2024/05/22 09:48

UITableView 的 表头背景 和 headerView跟随 cell一起滚动

这是我在做一个项目时遇到的问题:怎样像新浪微博或者QQ空间里的:“个人空间”那样,在表格最上方有个背景图片(把背景放在表头)?向下拖动时能显示更多的图片内容?向上拖时,表格的UITableViewStyle不用改为group,headerView也能跟随一起滚动?

1.向下拖动时能显示更多的图片内容

可以这样:设置UIEdgeInsetsMake(top, left, bottom, right);
,比如UIEdgeInsetsMake(-100,0,0,0)。效果是:表格的最上方插入一段高-100大小的空余,很明显-100就是表格显示时从表格内容高度100的地方开始显示,高度比100小的被隐藏了,只有向下拖动才能看到。(解决)


2.向上拖时,表格的UITableViewStyle不用改为group,headerView也能跟随cell一起滚动?

这个很简单,但是花了哥哥不少时间。(主要是各种“百” 、各种“度”,但是都没满意的做法,可能有经验的早就知道了。不过没分享出来,至少我找了好久都没找到!!)

只要:设section==0时,cell的数量为0 OK了,问题解决了。


链接

http://blog.csdn.net/tangaowen/article/details/6452314

他的第三个方法是什么意思,我没弄懂。

有个不满意地方:新浪微博的背景能拉伸,该怎么搞还得研究研究,如果你有好方法大笑



下面是代码:头文件定义一个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


原创粉丝点击