// // MTCommonListTableConfig.m // MTCommonListTable // // Created by ko1o on 2018/8/29. // Copyright © 2018年 ko1o. All rights reserved. // #import "MTCommonListTableConfig.h" #import @implementation MTCommonListTableCellConfig + (instancetype)defaultConfig { MTCommonListTableCellConfig *cellConfig = [[self alloc] init]; cellConfig.selectionStyle = UITableViewCellSelectionStyleNone; cellConfig.accessoryType = UITableViewCellAccessoryNone; cellConfig.selectionStyle = UITableViewCellSelectionStyleNone; cellConfig.accessoryType = UITableViewCellAccessoryNone; cellConfig.accessorySwitchButtonOnTintColor = THEME_COLOR; return cellConfig; } + (instancetype)configWithStyle:(MTCommonListTableCellStyle)style { MTCommonListTableCellConfig *cellConfig = [self defaultConfig]; cellConfig.style = style; return cellConfig; } - (id)copyWithZone:(nullable NSZone *)zone { MTCommonListTableCellConfig *cellConfig = [[[self class] allocWithZone:zone] init]; unsigned int propertyCount = 0; objc_property_t *propertyList = class_copyPropertyList([self class], &propertyCount); for (unsigned int i = 0; i < propertyCount; i++ ) { objc_property_t *thisProperty = &propertyList[i]; const char *propertyName = property_getName(*thisProperty); NSString *propertyKey = [NSString stringWithUTF8String:propertyName]; id value = [self valueForKeyPath:propertyKey]; if ([value conformsToProtocol:@protocol(NSCopying)]) { NSValue *copyVaule = [value copy]; [cellConfig setValue:copyVaule forKeyPath:propertyKey]; } else if ([value conformsToProtocol:@protocol(NSCoding)]){ id newValue = [NSKeyedArchiver archivedDataWithRootObject:value]; newValue = [NSKeyedUnarchiver unarchiveObjectWithData:newValue]; [cellConfig setValue:newValue forKeyPath:propertyKey]; } else if (value) { NSAssert(NO, @"%@需要实现copy %@",value,propertyKey); } cellConfig.style = self.style; } return cellConfig; } #pragma mark - getter - (CGFloat)separatorMarginLeft { return _separatorMarginLeft ?: _tableConfig.separatorMarginLeft; } - (MTCommonListTableCellSeparatorLeadingType)separatorLeadingType { return _separatorLeadingType ?: _tableConfig.cellSeparatorLeadingType; } - (CGFloat)imageCornerRadius { return _imageCornerRadius ?: _tableConfig.cellImageCornerRadius; } - (MTCommonListTableCellDidSelectBlock)cellAction { return _cellAction ?: _tableConfig.cellAction; } - (UIFont *)titleFont { return _titleFont ?: _tableConfig.cellTitleFont; } - (UIColor *)titleColor { return _titleColor ?: _tableConfig.cellTitleColor; } - (CGFloat)imageMarginLeft { return _imageMarginLeft ?: _tableConfig.cellImageMarginLeft; } - (UIColor *)backgroundColor { return _backgroundColor ?: _tableConfig.cellBackgroudColor; } - (UIColor *)imageTintColor { return _imageTintColor ?: _tableConfig.cellImageTintColor; } - (CGSize)imageSize { return !CGSizeEqualToSize(_imageSize, CGSizeZero) ? _imageSize : _tableConfig.cellImageSize; } - (UIFont *)accessoryTitleFont { return _accessoryTitleFont ?: _tableConfig.cellAccessoryTitleFont; } - (UIColor *)accessoryTitleColor { return _accessoryTitleColor ?: _tableConfig.cellAccessoryTitleColor; } - (NSString *)accessoryImageName { return _accessoryImageName ?: _tableConfig.cellAccessoryImageName; } - (NSString *)accessoryCheckMarkImageName { return _accessoryCheckMarkImageName ?: _tableConfig.cellAccessoryCheckMarkImageName; } - (NSString *)accessoryDisclosureIndicatorImageName { return _accessoryDisclosureIndicatorImageName ?: _tableConfig.cellAccessoryDisclosureIndicatorImageName; } - (NSString *)accessoryDetailImageName { return _accessoryDetailImageName ?: _tableConfig.cellAccessoryDetailImageName; } - (CGFloat)accessoryTitleAndImageMargin { return _accessoryTitleAndImageMargin ?: _tableConfig.cellAccessoryTitleAndImageMargin; } - (CGSize)accessoryImageSize { return !CGSizeEqualToSize(_accessoryImageSize, CGSizeZero) ? _accessoryImageSize : _tableConfig.cellAccessoryImageSize; } - (CGFloat)height { return _height ?: _customCell.frame.size.height ?: _tableConfig.cellHeight; } - (void)setAccessorySwitchButtonOnTintColor:(UIColor *)accessorySwitchButtonOnTintColor { _accessorySwitchButtonOnTintColor = accessorySwitchButtonOnTintColor; self.switchButton.onTintColor = accessorySwitchButtonOnTintColor; } - (void)setChecked:(BOOL)checked { _checked = checked; if (_style == MTCommonListTableCellStyleCheckmark) { _accessoryType = checked ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; } } - (void)setStyle:(MTCommonListTableCellStyle)style { switch (style) { case MTCommonListTableCellStyleNone: _selectionStyle = UITableViewCellSelectionStyleNone; _accessoryType = UITableViewCellAccessoryNone; break; case MTCommonListTableCellStyleDetailButton: { _accessoryType = UITableViewCellAccessoryDetailButton; _selectionStyle = UITableViewCellSelectionStyleGray; break; } case MTCommonListTableCellStyleCheckmark: { _accessoryType = _checked ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; _selectionStyle = UITableViewCellSelectionStyleGray; break; } case MTCommonListTableCellStyleDisclosureIndicator: { _accessoryType = UITableViewCellAccessoryDisclosureIndicator; _selectionStyle = UITableViewCellSelectionStyleGray; break; } case MTCommonListTableCellStyleSwitch: { UISwitch *switchButton = [[UISwitch alloc] init]; _switchButton = switchButton; [_switchButton addTarget:self action:@selector(switchButtonDidVualeChange:) forControlEvents:UIControlEventValueChanged]; _switchButton.onTintColor = _accessorySwitchButtonOnTintColor; _accessoryView = _switchButton; _selectionStyle = UITableViewCellSelectionStyleNone; break; } case MTCommonListTableCellStyleCenterLabelAction: { _selectionStyle = UITableViewCellSelectionStyleGray; _accessoryType = UITableViewCellAccessoryNone; break; } default: break; } _style = style; } - (void)switchButtonDidVualeChange:(UISwitch *)sender { if (self.switchButtonAction) { self.switchButtonAction(sender, _cell); } } @end @implementation MTCommonListTableSectionConfig + (instancetype)defaultConfig { MTCommonListTableSectionConfig *sectionConfig = [[self alloc] init]; return sectionConfig; } - (void)setHeaderTitle:(NSString *)headerTitle { if ([headerTitle isMemberOfClass:[MTNullElement class]]) return; _headerTitle = headerTitle; } - (void)setFooterTitle:(NSString *)footerTitle { if ([footerTitle isMemberOfClass:[MTNullElement class]]) return; _footerTitle = footerTitle; } - (void)setHeaderView:(UIView *)headerView { if ([headerView isMemberOfClass:[MTNullElement class]]) return; _headerView = headerView; _headerHeight = headerView.frame.size.height; } - (void)setFooterView:(UIView *)footerView { if ([footerView isMemberOfClass:[MTNullElement class]]) return; _footerView = footerView; _footerHeight = footerView.frame.size.height; } - (UIFont *)headerTitleFont { return _headerTitleFont ?: _tableConfig.sectionHeaderTitleFont; } - (UIColor *)headerTitleColor { return _headerTitleColor ?: _tableConfig.sectionHeaderTitleColor; } - (UIEdgeInsets)headerInset { return !UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, _headerInset) ? _headerInset : _tableConfig.sectionHeaderInset; } - (UIFont *)footerTitleFont { return _footerTitleFont ?: _tableConfig.sectionFooterTitleFont; } - (UIColor *)footerTitleColor { return _footerTitleColor ?: _tableConfig.sectionFooterTitleColor; } - (UIEdgeInsets)footerInset { return !UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, _footerInset) ? _footerInset : _tableConfig.sectionFooterInset; } - (CGFloat)headerHeight { if (self.headerTitle || self.footerAttributedTitle.string) { UILabel *tempLabel = [UILabel new]; if (self.headerAttributedTitle) { tempLabel.attributedText = self.headerAttributedTitle; } else { tempLabel.text = self.headerTitle; } [tempLabel sizeToFit]; _headerHeight = tempLabel.height + self.headerInset.top + self.headerInset.bottom; } return _headerHeight ?: _tableConfig.sectionHeaderHeight; } - (CGFloat)footerHeight { if (self.footerTitle || self.footerAttributedTitle.string) { UILabel *tempLabel = [UILabel new]; if (self.footerAttributedTitle) { tempLabel.attributedText = self.footerAttributedTitle; } else { tempLabel.text = self.footerTitle; } [tempLabel sizeToFit]; _footerHeight = tempLabel.height + self.footerInset.top + self.footerInset.bottom; } return _footerHeight ?: _tableConfig.sectionFooterHeight; } @end @interface MTCommonListTableConfig () @property (nonatomic, assign) MTCommonListTableCellStyle cellStyle; @property (nonatomic, strong) MTCommonListTableCellConfig *cellConfig; @property (nonatomic, assign) BOOL needSyncConfigToCellConfig; @end @implementation MTCommonListTableConfig + (instancetype)sharedConfig { static MTCommonListTableConfig *deafultConfig = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ deafultConfig = [self defaultConfig]; }); return deafultConfig; } - (id)copyWithZone:(NSZone *)zone { MTCommonListTableConfig *tableConfig = [[MTCommonListTableConfig allocWithZone:zone] init]; tableConfig.tableStyle = self.tableStyle; tableConfig.tableBackgroundColor = self.tableBackgroundColor; tableConfig.separatorStyle = self.separatorStyle; tableConfig.cellStyle = self.cellStyle; tableConfig.cellConfig = [MTCommonListTableCellConfig configWithStyle:tableConfig.cellStyle]; tableConfig.sectionHeaderHeight = self.sectionHeaderHeight; tableConfig.sectionFooterHeight = self.sectionFooterHeight; tableConfig.cellImageMarginLeft = self.cellImageMarginLeft; tableConfig.cellTitleAndImageMargin = self.cellTitleAndImageMargin; tableConfig.cellImageSize = self.cellImageSize; tableConfig.cellAccessoryTitleAndImageMargin = self.cellAccessoryTitleAndImageMargin; tableConfig.cellAccessoryImageSize = self.cellAccessoryImageSize; tableConfig.cellAccessoryTitleFont = self.cellAccessoryTitleFont; tableConfig.cellAccessoryTitleColor = self.cellAccessoryTitleColor; tableConfig.cellHeight = self.cellHeight; tableConfig.cellTitleFont = self.cellTitleFont; tableConfig.cellTitleColor = self.cellTitleColor; tableConfig.cellAccessoryDisclosureIndicatorImageName = self.cellAccessoryDisclosureIndicatorImageName; tableConfig.cellBackgroudColor = self.cellBackgroudColor; tableConfig.sectionHeaderInset = self.sectionHeaderInset; tableConfig.sectionHeaderTitleFont = self.sectionHeaderTitleFont; tableConfig.sectionHeaderTitleColor = self.sectionHeaderTitleColor; tableConfig.sectionFooterTitleFont = self.sectionFooterTitleFont; tableConfig.sectionFooterTitleColor = self.sectionFooterTitleColor; tableConfig.sectionFooterInset = self.sectionFooterInset; tableConfig.tableContentInset = self.tableContentInset; return tableConfig; } + (instancetype)defaultConfig { MTCommonListTableConfig *tableConfig = [[MTCommonListTableConfig alloc] init]; tableConfig.tableStyle = UITableViewStyleGrouped; tableConfig.tableBackgroundColor = [UIColor groupTableViewBackgroundColor]; tableConfig.separatorStyle = UITableViewCellSeparatorStyleSingleLine; tableConfig.cellStyle = MTCommonListTableCellStyleNone; tableConfig.cellConfig = [MTCommonListTableCellConfig configWithStyle:tableConfig.cellStyle]; tableConfig.sectionHeaderHeight = 20.0; tableConfig.sectionFooterHeight = 0.1; tableConfig.cellImageMarginLeft = 16; tableConfig.cellTitleAndImageMargin = 16; tableConfig.cellImageSize = CGSizeMake(25, 25); tableConfig.cellAccessoryTitleAndImageMargin = 10; tableConfig.cellAccessoryImageSize = CGSizeMake(9, 16); tableConfig.cellAccessoryTitleFont = [UIFont systemFontOfSize:16]; tableConfig.cellAccessoryTitleColor = [UIColor grayColor]; tableConfig.cellHeight = 44.0; tableConfig.cellTitleFont = [UIFont systemFontOfSize:18]; tableConfig.cellTitleColor = [UIColor blackColor]; tableConfig.cellAccessoryDisclosureIndicatorImageName = @"cell_arrow_right"; tableConfig.sectionHeaderInset = UIEdgeInsetsMake(10, 16, 10, 16); tableConfig.sectionHeaderTitleFont = [UIFont systemFontOfSize:16]; tableConfig.sectionHeaderTitleColor = [UIColor grayColor]; tableConfig.sectionFooterTitleFont = [UIFont systemFontOfSize:16]; tableConfig.sectionFooterTitleColor = [UIColor grayColor]; tableConfig.sectionFooterInset = UIEdgeInsetsMake(10, 16, 10, 16); tableConfig.tableContentInset = UIEdgeInsetsMake(0, 0, 20, 0); return tableConfig; } #pragma mark - Public + (instancetype)defaultConfigWithRowsCountForSections:(NSArray *)rowsCountArr { return [self defaultConfigWithCellConfig:[MTCommonListTableCellConfig configWithStyle:MTCommonListTableCellStyleNone] rowsCountForSections:rowsCountArr]; } + (instancetype)defaultConfigWithCellStyle:(MTCommonListTableCellStyle)style rowsCountForSections:(NSArray *)rowsCountArr { return [self defaultConfigWithCellConfig:[MTCommonListTableCellConfig configWithStyle:style] rowsCountForSections:rowsCountArr]; } + (instancetype)defaultConfigWithCellConfig:(MTCommonListTableCellConfig *)cellConfig rowsCountForSections:(NSArray *)rowsCountArr { MTCommonListTableConfig *tableConfig = [[self sharedConfig] copy]; NSMutableArray *sectionConfigsM = [NSMutableArray array]; for (NSNumber *rowsCount in rowsCountArr) { MTCommonListTableSectionConfig *sectionConfig = [MTCommonListTableSectionConfig defaultConfig]; sectionConfig.tableConfig = tableConfig; NSMutableArray *cellConfigsM = [NSMutableArray array]; for (int i = 0; i < [rowsCount integerValue]; i++) { MTCommonListTableCellConfig *copyCellConfig = [cellConfig copy]; copyCellConfig.tableConfig = tableConfig; [cellConfigsM addObject:copyCellConfig]; } sectionConfig.cellConfigs = [cellConfigsM mutableCopy]; [sectionConfigsM addObject:sectionConfig]; } tableConfig.sectionConfigs = [sectionConfigsM mutableCopy]; return tableConfig; } - (void)resetCellConfigForSection:(NSInteger)section row:(NSInteger)row config:(void(^)(MTCommonListTableCellConfig *config))config { if (config) { config([self cellConfigForSection:section row:row]); } } - (void)resetCellConfigForRowsAtIndexPaths:(NSArray *)indexPaths config:(void(^)(MTCommonListTableCellConfig *config, NSIndexPath *indexPath))config { if (config) { for (NSIndexPath *indexPath in indexPaths) { if (config) { config([self cellConfigForSection:indexPath.section row:indexPath.row],indexPath); } // [self resetCellConfigForSection:indexPath.section row:indexPath.row config:config]; } } } - (MTCommonListTableCellConfig *)cellConfigForSection:(NSInteger)section row:(NSInteger)row { NSAssert(_sectionConfigs, @"sectionConfigs is nil!"); MTCommonListTableSectionConfig *sectionConfig = _sectionConfigs[section]; return sectionConfig.cellConfigs[row]; } - (void)resetSectionConfigForSection:(NSInteger)section config:(void(^)(MTCommonListTableSectionConfig *config))config { if (config) { config([self sectionConfigForSection:section]); } } - (void)resetSectionConfigForSections:(NSArray *)sections config:(void(^)(MTCommonListTableSectionConfig *config))config { if (config) { for (NSNumber *section in sections) { [self resetSectionConfigForSection:[section integerValue] config:config]; } } } - (MTCommonListTableSectionConfig *)sectionConfigForSection:(NSInteger)section { NSAssert(_sectionConfigs, @"sectionConfigs is nil!"); return _sectionConfigs[section]; } #pragma mark - setting - (void)setSectionHeaderTitles:(NSArray *)sectionHeaderTitles { _sectionHeaderTitles = sectionHeaderTitles; for (int i = 0; i < sectionHeaderTitles.count; i++) { ((MTCommonListTableSectionConfig *)self.sectionConfigs[i]).headerTitle = sectionHeaderTitles[i]; } } - (void)setSectionFooterTitles:(NSArray *)sectionFooterTitles { _sectionFooterTitles = sectionFooterTitles; for (int i = 0; i < sectionFooterTitles.count; i++) { ((MTCommonListTableSectionConfig *)self.sectionConfigs[i]).footerTitle = sectionFooterTitles[i]; } } - (void)setCellTitles:(NSArray *)cellTitles { _cellTitles = cellTitles; NSInteger index = 0; for (MTCommonListTableSectionConfig *sectionConfig in self.sectionConfigs) { for (MTCommonListTableCellConfig *cellConfig in sectionConfig.cellConfigs) { if (![cellTitles[index] isMemberOfClass:[NSNull class]]) { cellConfig.title = cellTitles[index]; } index++; } } } - (void)setCellImageSize:(CGSize)cellImageSize { _cellImageSize = cellImageSize; for (MTCommonListTableSectionConfig *sectionConfig in self.sectionConfigs) { for (MTCommonListTableCellConfig *cellConfig in sectionConfig.cellConfigs) { cellConfig.imageSize = cellImageSize; } } } - (void)setCellImageMarginLeft:(CGFloat)cellImageMarginLeft { _cellImageMarginLeft = cellImageMarginLeft; for (MTCommonListTableSectionConfig *sectionConfig in self.sectionConfigs) { for (MTCommonListTableCellConfig *cellConfig in sectionConfig.cellConfigs) { cellConfig.imageMarginLeft = cellImageMarginLeft; } } } - (void)setCllTitleAndImageMargin:(CGFloat)cellTitleAndImageMargin { _cellTitleAndImageMargin = cellTitleAndImageMargin; for (MTCommonListTableSectionConfig *sectionConfig in self.sectionConfigs) { for (MTCommonListTableCellConfig *cellConfig in sectionConfig.cellConfigs) { cellConfig.titleAndImageMargin = cellTitleAndImageMargin; } } } - (void)setCellImageNames:(NSArray *)cellImageNames { _cellImageNames = cellImageNames; NSInteger index = 0; for (MTCommonListTableSectionConfig *sectionConfig in self.sectionConfigs) { for (MTCommonListTableCellConfig *cellConfig in sectionConfig.cellConfigs) { if (![cellImageNames[index] isMemberOfClass:[NSNull class]]) { cellConfig.iconImageName = cellImageNames[index]; } index++; } } } @end