UITableView使Header不再懸浮之后,首個(gè)header出現(xiàn)在屏幕外

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

define kRowH 40

define kSectionH 50

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"年后";
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Yosemite"]];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

    _tableView = [[UITableView alloc] initWithFrame:(CGRect){CGPointZero,{CGRectGetWidth(self.view.frame),CGRectGetHeight(self.view.frame)-64}}];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = kRowH;
    _tableView.sectionHeaderHeight = kSectionH;
    _tableView.tableFooterView = [UIView new];
    _tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iphone6"]];
    [self.view addSubview:_tableView];
    }

pragma mark - TableView

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 12;
    }
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"12"];
    if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"12"];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@-%@",@(indexPath.section),@(indexPath.row)];
    return cell;
    }
  • (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [NSString stringWithFormat:@"%@-------------",@(section)];
    }

pragma mark - ScrollView

  • (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = kSectionH;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
    scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
    scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }

    if (ABS(scrollView.contentOffset.y) < 30) {
    if ([scrollView isKindOfClass:[UITableView class]]) {
    CGFloat scale = 1-ABS(scrollView.contentOffset.y)/30;
    _tableView.backgroundView.transform = CGAffineTransformMakeScale(scale, scale);
    }
    }

}

  • (void)scrollViewDidEndDecelerating:(UIScrollView )scrollView {
    if (scrollView.contentSize.height < CGRectGetHeight(scrollView.frame)) {
    [scrollView setContentOffset:CGPointZero animated:NO];
    }
    NSLog(@"*********\noffset is %f,\n height is %f\n,
    **\n ivewH is %f",scrollView.contentOffset.y,scrollView.contentSize.height,scrollView.frame.size.height);
    }

  • (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (!decelerate) {
    if (scrollView.contentSize.height < CGRectGetHeight(scrollView.frame)) {
    [scrollView setContentOffset:CGPointZero animated:YES];
    }
    }

    NSLog(@"*********\noffset is %f,\n height is %f\n,***\n ivewH is %f",scrollView.contentOffset.y,scrollView.contentSize.height,scrollView.frame.size.height);
    }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容