用tableView实现的一种加载数据的布局

简介:

用tableView实现的一种加载数据的布局

此博文是应朋友之邀解决他的业务逻辑问题

效果:

素材:

源码:

ImageCell.h 与 ImageCell.m

//
//  ImageCell.h
//  TableView
//
//  Created by YouXianMing on 15/2/1.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ImageCell : UITableViewCell

@end


//
//  ImageCell.m
//  TableView
//
//  Created by YouXianMing on 15/2/1.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "ImageCell.h"

@implementation ImageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -72, 320, 72 * 2)];
        imageView.image        = [UIImage imageNamed:@"1"];
        [self addSubview:imageView];
    }
    
    return self;
}

@end

ViewController.m
//
//  ViewController.m
//  TableView
//
//  Created by YouXianMing on 15/2/1.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "ImageCell.h"

#define CELL_FLAG  @"Cell"
#define IMAG_FLAG  @"Imag"

typedef enum : NSUInteger {
    SECTION_ONE = 0,
    SECTION_TWO,
    
    MAX_SECTION,
} EViewController;

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView      *blockView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    CGRect rect       = self.view.bounds;
    rect.origin.y    += 72;
    rect.size.height -= 72;
    
    self.tableView            = [[UITableView alloc] initWithFrame:rect];
    self.tableView.delegate   = self;
    self.tableView.dataSource = self;
    self.tableView.layer.masksToBounds = NO;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CELL_FLAG];
    [self.tableView registerClass:[ImageCell class] forCellReuseIdentifier:IMAG_FLAG];
    [self.view addSubview:self.tableView];
    
    self.blockView                 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 72)];
    self.blockView.backgroundColor = [UIColor blackColor];
    self.blockView.alpha           = 0.f;
    [self.view addSubview:self.blockView];
}
#pragma mark scrollView位移
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offsetY = scrollView.contentOffset.y;

    CGFloat percent = offsetY / 72.f;
    if (percent <= 0) {
        percent = 0;
    } else if (percent >= 1) {
        percent = 1;
    }
    
    self.blockView.alpha = percent;
}
#pragma mark 每个区row的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == SECTION_ONE) {
        return 1;
    } else if (section == SECTION_TWO) {
        return 7;
    } else {
        return 0;
    }
}
#pragma mark 几个区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return MAX_SECTION;
}
#pragma mark 重用cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == SECTION_ONE) {
        ImageCell *cell = [tableView dequeueReusableCellWithIdentifier:IMAG_FLAG];
        return cell;
    } else if (indexPath.section == SECTION_TWO) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_FLAG];
        return cell;
    } else {
        return nil;
    }
}
#pragma mark 返回headerView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == SECTION_ONE) {
        return nil;
    } else if (section == SECTION_TWO) {
        UIView *view         = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
        view.backgroundColor = [UIColor redColor];
        return view;
    } else {
        return nil;
    }
}
#pragma mark row高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == SECTION_ONE) {
        return 72;
    } else if (indexPath.section == SECTION_TWO) {
        return 200;
    } else {
        return 0;
    }
}
#pragma mark header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == SECTION_ONE) {
        return 0.f;
    } else if (section == SECTION_TWO) {
        return 40;
    } else {
        return 0;
    }
}

@end

目录
相关文章
|
10月前
|
数据采集 算法 vr&ar
基于国产化芯片的神经腕带技术方案,实现META神经腕带效果,创新交互方式
唯理科技发布了一款基于自研WLS128芯片的神经腕带产品,能够通过采集前臂肌肉神经电与肢体运动信息实现自然手势识别。该产品具备微弱肌电信号采集、多通道SEMG数据采集、低功耗设计、高采样率、专业算法支持等特性,支持多种数据格式导出及第三方数据接口,应用场景广泛。唯理科技是国内少数拥有自研芯片的脑电脑机接口技术厂商,致力于为客户提供一体化的软硬件解决方案。
|
11天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1230 5
|
10天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1214 87
|
10天前
|
云栖大会
阿里云云栖大会2025年9月24日开启,免费申请大会门票,速度领取~
2025云栖大会将于9月24-26日举行,官网免费预约畅享票,审核后短信通知,持证件入场
1796 13
|
20天前
|
人工智能 运维 安全