// // MTCommonListTableViewCell.m // MTCommonListTable // // Created by ko1o on 2018/8/29. // Copyright © 2018年 ko1o. All rights reserved. // #import "MTCommonListTableViewCell.h" #import "MTCommonListTableConfig.h" @implementation MTCommonListTableViewCell + (instancetype)cellWithConfig:(MTCommonListTableCellConfig *)config { MTCommonListTableViewCell *cell = [[super alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil]; cell.cellConfig = config; return cell; } - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { } - (void)setCellConfig:(MTCommonListTableCellConfig *)cellConfig { _cellConfig = cellConfig; self.selectionStyle = cellConfig.selectionStyle; self.accessoryType = cellConfig.accessoryType; self.backgroundColor = cellConfig.backgroundColor; self.textLabel.text = cellConfig.title; self.textLabel.font = cellConfig.titleFont; self.textLabel.textColor = cellConfig.titleColor; self.detailTextLabel.text = cellConfig.accessoryTitle; self.detailTextLabel.textColor = cellConfig.accessoryTitleColor; self.detailTextLabel.font = cellConfig.accessoryTitleFont; self.imageView.image = cellConfig.iconImageName ? ImageNamed(cellConfig.iconImageName) : nil; NSString *accessoryImageName = cellConfig.accessoryImageName; switch (cellConfig.style) { case MTCommonListTableCellStyleCheckmark: { accessoryImageName = cellConfig.accessoryCheckMarkImageName; break; } case MTCommonListTableCellStyleDetailButton: { accessoryImageName = cellConfig.accessoryDetailImageName; break; } case MTCommonListTableCellStyleDisclosureIndicator: { accessoryImageName = cellConfig.accessoryDisclosureIndicatorImageName; break; } case MTCommonListTableCellStyleNone: { accessoryImageName = nil; break; } default: break; } self.accessoryView = accessoryImageName ? [[UIImageView alloc] initWithImage:[UIImage imageNamed:accessoryImageName]] : cellConfig.accessoryView; if (cellConfig.accessoryView) { UIView *accessoryView = cellConfig.accessoryView; accessoryView.x = [UIScreen mainScreen].bounds.size.width - accessoryView.frame.size.width - 35; [self.contentView addSubview:accessoryView]; } cellConfig.cell = self; } - (void)layoutSubviews { [super layoutSubviews]; MTCommonListTableCellConfig *cellConfig = self.cellConfig; if (self.imageView.image) { self.imageView.frame = CGRectMake(cellConfig.imageMarginLeft, (self.cellConfig.height - cellConfig.imageSize.height) * 0.5, cellConfig.imageSize.width, cellConfig.imageSize.height); self.imageView.layer.cornerRadius = self.cellConfig.imageCornerRadius; self.imageView.clipsToBounds = YES; } self.contentView.backgroundColor = [UIColor clearColor]; self.backgroundColor = self.cellConfig.backgroundColor; if (cellConfig.accessoryView && (cellConfig.accessoryType == MTCommonListTableCellStyleDisclosureIndicator || cellConfig.accessoryType == MTCommonListTableCellStyleDisclosureIndicator)) { cellConfig.accessoryView.centerY = self.height * 0.5; } self.textLabel.frame = CGRectMake(CGRectGetMaxX(self.imageView.frame) + cellConfig.imageMarginLeft, self.textLabel.frame.origin.y, self.textLabel.frame.size.width, self.textLabel.frame.size.height); if ([self.accessoryView isMemberOfClass:[UIImageView class]]) { self.accessoryView.frame = CGRectMake(CGRectGetMaxX(self.accessoryView.frame) - cellConfig.accessoryImageSize.width, (self.cellConfig.height - cellConfig.accessoryImageSize.height) * 0.5, cellConfig.accessoryImageSize.width, cellConfig.accessoryImageSize.height); self.detailTextLabel.frame = CGRectMake(self.accessoryView.frame.origin.x - cellConfig.accessoryTitleAndImageMargin - self.detailTextLabel.frame.size.width, self.detailTextLabel.frame.origin.y, self.detailTextLabel.frame.size.width, self.detailTextLabel.frame.size.height); } if (cellConfig.style == MTCommonListTableCellStyleCenterLabelAction) { self.textLabel.frame = self.bounds; self.textLabel.textAlignment = NSTextAlignmentCenter; } for (UIView *cellSubView in self.subviews) { if ([NSStringFromClass([cellSubView class]) isEqualToString:@"_UITableViewCellSeparatorView"]) { if (cellSubView.frame.origin.y <= 0) { cellSubView.hidden = YES; } // if (cellSubView.frame.origin.x <= 0) break; CGFloat marginLeft = 0.0; cellSubView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.1]; cellConfig.separatorLeadingType = MTCommonListTableCellSeparatorLeadingTypeLeadingTitle; switch (cellConfig.separatorLeadingType) { case MTCommonListTableCellSeparatorLeadingTypeLeadLeftmostItem: { if (self.imageView.image) { marginLeft = self.imageView.frame.origin.x; } else if (self.textLabel.text) { marginLeft = self.textLabel.frame.origin.x; } break; } case MTCommonListTableCellSeparatorLeadingTypeLeadingImage: { if (self.imageView.image) { marginLeft = self.imageView.frame.origin.x; } break; } case MTCommonListTableCellSeparatorLeadingTypeLeadingTitle: { if (self.textLabel.text) { marginLeft = self.textLabel.frame.origin.x; } break; } default: break; } marginLeft = _cellConfig.separatorMarginLeft ?: marginLeft; cellSubView.frame = CGRectMake(marginLeft, cellSubView.frame.origin.y, self.frame.size.width * 2, cellSubView.frame.size.height); } } if (cellConfig.didLayoutSubviewsAction) { cellConfig.didLayoutSubviewsAction(self); } } @end