ReplyDetailView.m 4.7 KB
//
//  ReplyDetailView.m
//  DreamSleep
//
//  Created by peter on 2022/10/9.
//

#import "ReplyDetailView.h"
#import "MajorCommentCell.h"
#import "ReplyCell.h"

#define kSectionHeaderHeight 48
#define kSectionFooterHeight 8

@interface ReplyDetailView () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSArray *detailListArr;
@end

@implementation ReplyDetailView

- (instancetype)initWithDelegate:(id<ReplyDetailViewDelegate>)replyDelegate {
    if (self = [super initWithFrame:CGRectZero style:UITableViewStyleGrouped]) {
        self.replyDelegate = replyDelegate;
        self.detailListArr = [NSArray array];
        
        self.delegate = self;
        self.dataSource = self;
        self.showsVerticalScrollIndicator = NO;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self registerClass:[MajorCommentCell class] forCellReuseIdentifier:NSStringFromClass([MajorCommentCell class])];
        [self registerClass:[ReplyCell class] forCellReuseIdentifier:NSStringFromClass([ReplyCell class])];
        self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 24 + Bottom_SafeArea_Height)];;
        self.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, DarkColor, DSWhite);
    }
    return self;
}

#pragma mark - public
- (void)updateReplyDetailView:(NSArray *)replyGroupData {
    if (replyGroupData.count) {
        self.detailListArr = replyGroupData;
        [self reloadData];
    }
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.detailListArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        NSArray *replyList = self.detailListArr[section];
        return replyList.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        CommentModel *commentModel = self.detailListArr[0];
        return [commentModel cellHeight];
    } else {
        NSArray *replyList = self.detailListArr[indexPath.section];
        ReplyModel *replyModel = replyList[indexPath.row];
        return [replyModel cellHeight];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return section == 0 ? CGFLOAT_MIN : kSectionHeaderHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0)];
    header.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, DarkColor, DSWhite);
    
    if (section == 0) {
        header.height = 0;
    } else {
        UILabel *totalReplyCountLab = [UILabel labWithFont:BoldFont(15)];
        totalReplyCountLab.dk_textColorPicker = DKColorPickerWithKey(Dk_TITLE);
        totalReplyCountLab.origin = CGPointMake(30, 15);
        [header addSubview:totalReplyCountLab];
        
        header.height = kSectionHeaderHeight;
        NSArray *replyList = self.detailListArr[section];
        totalReplyCountLab.text = replyList.count ? [NSString stringWithFormat:@"全部回复(%ld)", replyList.count] : @"暂无回复";
        [totalReplyCountLab sizeToFit];
    }
    
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return section == self.detailListArr.count - 1 ? CGFLOAT_MIN : kSectionFooterHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, section == self.detailListArr.count - 1 ? CGFLOAT_MIN : kSectionFooterHeight)];
    footer.dk_backgroundColorPicker = DKColorPickerWithColors(BGColor, AlertDarkColor, DSWhite);
    return footer;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        MajorCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MajorCommentCell class]) forIndexPath:indexPath];
        cell.commentModel = self.detailListArr[indexPath.section];
        return cell;
    } else {
        ReplyCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ReplyCell class]) forIndexPath:indexPath];
        NSArray *replyList = self.detailListArr[indexPath.section];
        ReplyModel *replyModel = replyList[indexPath.row];
        cell.replyModel = replyModel;
        return cell;
    }
}

@end