HomeTableView.m 2.8 KB
//
//  HomeTableView.m
//  DreamSleep
//
//  Created by peter on 2022/4/8.
//

#import "HomeTableView.h"
#import "DSGifHeader.h"
#import "HomeTableViewCell.h"
#import "HomeHeaderView.h"

@interface HomeTableView () <UITableViewDelegate>
@property (nonatomic, strong) HomeHeaderView *headerView;
@property (nonatomic, strong) DSDataSource *homeDataSource;
@end

@implementation HomeTableView

- (instancetype)initDemo {
    if (self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain]) {
        self.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
        [self.homeDataSource addDataArray:@[@1, @2, @3]];
        self.mj_header = [DSGifHeader headerWithRefreshingBlock:^{
            dispatch_after(5.0, dispatch_get_main_queue(), ^{
                [self.mj_header endRefreshing];
            });
        }];
    }
    return self;
}

- (DSDataSource *)homeDataSource {
    if (!_homeDataSource) {
        CellConfigureBlock cellBlock = ^(HomeTableViewCell * cell, id model, NSIndexPath * indexPath) {
            cell.textLabel.text = @"chengx";
            cell.backgroundColor = BrandColor;
            // [cell configureCell:model];
        };
        NSString * const homeCellIdentifier = @"HomeCellIdentifier";
        _homeDataSource = [[DSDataSource alloc] initWithIdentifier:homeCellIdentifier datas:@[] isSection:NO configureBlock:cellBlock];
        self.dataSource = _homeDataSource;
        self.delegate = self;
        [self registerClass:[HomeTableViewCell class] forCellReuseIdentifier:homeCellIdentifier];
        
        self.tableHeaderView = self.headerView;
    }
    return _homeDataSource;
}

- (HomeHeaderView *)headerView {
    if (!_headerView) {
        // banner高度
        CGFloat bannerH = 2*(kScreenWidth - 48)/5.0;
        // 三分钟即刻入睡按钮宽度
        CGFloat width = (kScreenWidth - 2*15 - 14)/2;
        CGFloat height = 15 + bannerH + 24 + 133*width/165.0;
        _headerView = [[HomeHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, height)];
        _headerView.backgroundColor = self.backgroundColor;
    }
    return _headerView;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0.001)];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0.001)];
}

@end