tableview上下拉动头视图跟着收缩效果

2016-07-12  本文已影响0人  CTBOY
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //创建头视图
    UIImageView * imageView = [UIImageView new];
    _tableView.contentInset = UIEdgeInsetsMake(imageViewH * 0.5, 0, 0, 0);
    imageView.frame = CGRectMake(0, -imageViewH, ScreenWidth, imageViewH);
    imageView.image = [UIImage imageNamed:@"xifuer.jpeg"];
    imageView.contentMode = UIViewContentModeScaleToFill;
    [_tableView insertSubview:imageView atIndex:0];
    self.imageView = imageView;
    [self.view addSubview:_tableView];
    
}
#pragma mark - 解决tableview分割线短一截问题
- (void)viewDidLayoutSubviews{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
    {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

#pragma mark - tableView代理方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"123"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"123"];
        cell.textLabel.text = @"123";
    }
    
    return cell;
}

#pragma mark - 借助scrollview的代理方法实现视图收缩放大
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat down = -(imageViewH*0.5) - scrollView.contentOffset.y;
    if (down < 0) return;
    
    CGRect frame = _imageView.frame;
    frame.size.height = imageViewH + down * 0.5;
    frame.size.width = ScreenWidth + down * 0.5;
    _imageView.frame = frame;
}

上一篇下一篇

猜你喜欢

热点阅读