//
//  CommonTableView.m
//  ChineseAgri-businesses
//
//  Created by ecell on 2022/6/28.
//

#import "CommonTableView.h"

@interface CommonTableView ()<UITableViewDelegate,UITableViewDataSource>


/// 声明自定义类型变量
@property(nonatomic,copy)CreateCell createCell;

/// 声明自定义类型变量
@property (nonatomic,copy)SelectCell selectBlock;


@property (nonatomic,copy)disScrollView didscorllView;

@property (nonatomic ,strong) Class commonCell;



@end

@implementation CommonTableView

/// UITableView初始化加载
/// @param frame UITableView坐标大小
/// @param style 样式
/// @param cellHeight 行高
/// @param cellRow 行数
/// @param isAdaptive 行高是否自适应
/// @param isLine 是否显示间隔线
/// @param commonCell 加载Cell
/// @param createCell 返回创建的cell
/// @param selectBlock cell点击事件
- (instancetype)initWithFrame:(CGRect)frame
                        style:(UITableViewStyle)style
                   cellHeight:(CGFloat)cellHeight
                      cellRow:(NSInteger)cellRow
                   isAdaptive:(BOOL)isAdaptive
                       isLine:(BOOL)isLine
                   commonCell:(Class)commonCell
                   createCell:(CreateCell)createCell
                 selectedCell:(SelectCell)selectBlock
                DidscrollView:(disScrollView)ScrollView
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
        //self.separatorColor = KKLineColor;
        if (@available(iOS 11.0, *)){
            [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
        }
        //self.backgroundColor = KKClearColor;
        //初始化tableview时,添加代码如下
        if (isAdaptive)
            self.estimatedRowHeight = Adapted(cellHeight);
        else
            self.rowHeight = Adapted(cellHeight);
        self.estimatedSectionHeaderHeight = 0;
        self.estimatedSectionFooterHeight = 0;
        self.isEditing = NO;
        if (!isLine)
            self.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [self registerClass:commonCell.class forCellReuseIdentifier:NSStringFromClass(commonCell.class)];
        self.showsVerticalScrollIndicator = NO;
        
        self.createCell = createCell;
        self.selectBlock = selectBlock;
        self.commonCell = commonCell;
        self.didscorllView = ScrollView;
        self.tableFooterView = [[UIView alloc]init];
        self.cellRow = cellRow;
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_14_5
    if (@available(iOS 15.0, *)) {
        self.sectionHeaderTopPadding = 0;
    }
#endif

    }
    return self;
}

- (void)setCellRow:(NSInteger)cellRow
{
    _cellRow = cellRow;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZZTableViewCell *cell = (ZZTableViewCell *)self.commonCell;
    cell = [self.commonCell cellWithTableView:tableView indexPath:indexPath];
    self.createCell(cell, indexPath);
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.selectBlock(tableView,indexPath);
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.cellRow;
}



#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.didscorllView(scrollView);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return Adapted(self.footerViewHeight);
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return self.footerView;
}


//使用系统默认的删除按钮
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        if ([self.delegates respondsToSelector:@selector(CommTableView:commitEditingStyle:forRowAtIndexPath:)])
        {
            [self.delegates CommTableView:tableView commitEditingStyle:editingStyle forRowAtIndexPath:indexPath];
        }
    }
}
//自定义系统默认的删除按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
 return GJText(@"删除");
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{    // 删除
    return self.isEditing ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleNone;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end