// // ViewController.m // detailView // // Created by 殷婷婷 on 16/5/20. // Copyright © 2016年 yin. All rights reserved. // #import "ViewController.h" @interface ViewController ()<UITableViewDelegate,UITableViewDataSource> @property (weak, nonatomic) IBOutlet UITableView *tableView; @property(nonatomic,weak) UIImageView *imgV; @end static CGFloat tableHeaderViewH = 300; @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, -tableHeaderViewH, self.view.bounds.size.width, tableHeaderViewH)]; imgV.image = [UIImage imageNamed:@"1.jpg"]; imgV.contentMode = UIViewContentModeScaleAspectFill; //self.tableView.tableHeaderView = imgV; self.imgV = imgV; [self.tableView insertSubview:imgV atIndex:0]; self.tableView.contentInset = UIEdgeInsetsMake(tableHeaderViewH * 0.5, 0, 0, 0);
//此时tableview的contentoffset.y = - tableHeaderViewH * 0.5,scrollDidscroll打印。。。 } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 20; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static int i = 1; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"]; } cell.textLabel.text = [NSString stringWithFormat:@"%d",i]; i++; return cell; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ NSLog(@"%f", -scrollView.contentOffset.y - tableHeaderViewH * 0.5); CGFloat delta = -scrollView.contentOffset.y - tableHeaderViewH * 0.5; if (delta <= 0) { [self.tableView bringSubviewToFront:self.imgV]; CGRect frame = self.imgV.frame; frame.origin.y = -tableHeaderViewH - delta; self.imgV.frame = frame; return; } [self.tableView insertSubview:self.imgV atIndex:0]; CGRect frame = self.imgV.frame; frame.size.height = tableHeaderViewH + delta; self.imgV.frame = frame; } @end