by SevenJohs.
武汉
关于编程思维,要学习的真的很多。
就今天走的弯路,老大看不下去,就修改了一下。
之前傻傻地写了一堆
//错误示范
/**
* 初始化对象会调用(添加子控件)
*/
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_colorLabel = [[UILabel alloc]init];
[self.contentView addSubview:_colorLabel];
_nameLabel = [[UILabel alloc]init];
[self.contentView addSubview:_nameLabel];
_valueLabel = [[UILabel alloc]init];
[self.contentView addSubview:_valueLabel];
_daytimeValueLabel = [[UILabel alloc]init];
[self.contentView addSubview:_daytimeValueLabel];
_nightValueLabel = [[UILabel alloc]init];
[self.contentView addSubview:_nightValueLabel];
_unitLabel = [[UILabel alloc]init];
[self.contentView addSubview:_unitLabel];
self.checkBoxBtn = (QWCheckBox *)[[UIButton alloc]init];
[self.contentView addSubview:self.checkBoxBtn];
// self.selectionStyle = UITableViewCellSelectionStyleNone;
}
return self;
}
/**
* 根据父视图的大小设置color的frame,并传入数据
* @param rect 父视图frame
* @param data 数据
*/
-(void)updateColorLabelSubviewsFrame:(CGRect)rect data:(QWPlanInfo *)data{
CGFloat startX = 0;
CGFloat startY = 0;
CGFloat width = 30;
CGFloat height = 10;
startX = EGDE_MARGIN;
startY = (rect.size.height - height)/2;
_data = data;
self.colorLabel.frame = CGRectMake(startX, startY,width, height);
self.colorLabel.backgroundColor = [QWColorTransfer colorWithHexString:data.color];;
}
/**
* 根据父视图的大小设置CheckBox的frame,并传入数据
* @param rect 父视图frame
* @param data 数据
*/
-(void)updateCheckBoxSubviewsFrame:(CGRect)rect data:(QWPlanInfo *)data{
CGFloat width = 30;
CGFloat height = 12;
CGFloat startX = (rect.size.width - EGDE_MARGIN*2)/2.5 * 2 ;
CGFloat startY = (rect.size.height - height)/2;
self.checkBoxBtn.frame = CGRectMake(startX, startY,width, height);
self.checkBoxBtn.titleLabel.text = @"显示";
[self.checkBoxBtn addTarget:self action:@selector(checkBoxClickAction) forControlEvents:UIControlEventTouchUpInside];
}
/**
* 根据父视图的大小设置nameLabel的frame,并传入数据
* @param rect 父视图frame
* @param data 数据
*/
-(void)updateNameLabelSubviewsFrame:(CGRect)rect data:(QWPlanInfo *)data{
CGSize nameLabelTextSize = [self sizeWithString:data.name font:MY_FONT maxSize:CGSizeMake(MAXFLOAT , MAXFLOAT)];
CGFloat width = nameLabelTextSize.width;
CGFloat height = nameLabelTextSize.height;
CGFloat startX = (rect.size.width - EGDE_MARGIN*2)/2.5 - width - EGDE_MARGIN;
CGFloat startY = (rect.size.height - height)/2;
_data = data;
self.nameLabel.frame = CGRectMake(startX, startY,width, height);
}
/**
* 根据父视图的大小设置valueLabel的frame,并传入数据
* @param rect 父视图frame
* @param data 数据
*/
-(void)updateValueLabelSubviewsFrame:(CGRect)rect data:(QWPlanInfo *)data{
CGSize valueLabelTextSize = [self sizeWithString:data.value font:MY_FONT maxSize:CGSizeMake(MAXFLOAT , MAXFLOAT)];
CGFloat width = valueLabelTextSize.width;
CGFloat height = valueLabelTextSize.height;
CGFloat startX = (rect.size.width - EGDE_MARGIN*2)/2.5;
CGFloat startY = (rect.size.height - height)/2;
_data = data;
self.valueLabel.frame = CGRectMake(startX, startY,width, height);
}
/**
* 根据父视图的大小设置unit的frame,并传入数据
* @param rect 父视图frame
* @param data 数据
*/
-(void)updatenUnitLabelSubviewsFrame:(CGRect)rect data:(QWPlanInfo *)data{
CGSize unitLabelTextSize = [self sizeWithString:data.unit font:MY_FONT maxSize:CGSizeMake(MAXFLOAT , MAXFLOAT)];
CGFloat width = unitLabelTextSize.width;
CGFloat height = unitLabelTextSize.height;
CGFloat startX = (rect.size.width - EGDE_MARGIN*2)/2.5 - width ;
CGFloat startY = (rect.size.height - height)/2;
_data = data;
self.unitLabel.frame = CGRectMake(startX, startY,width, height);
}
/**
* 计算文本宽高
*
* @return 文本宽高
*/
-(CGSize)sizeWithString:(NSString*)str font:(UIFont*)font maxSize:(CGSize)maxSize{
CGSize size = [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil].size;
return size;
}
虽然以上可以显示需求的大小,但是调用时还要判断cell风格,会疯,而且耦合度太高。
//改进
/**
* 初始化对象会调用(添加子控件)
*/
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_colorLabel = [[UILabel alloc]init];
[self.contentView addSubview:_colorLabel];
_nameLabel = [[UILabel alloc]init];
[self.contentView addSubview:_nameLabel];
_dayImageView1= [[UIImageView alloc]init];
[self.contentView addSubview:_dayImageView1];
_dayImageView2= [[UIImageView alloc]init];
[self.contentView addSubview:_dayImageView2];
_valueLabel = [[UILabel alloc]init];
[self.contentView addSubview:_valueLabel];
_nightImageView1 = [[UIImageView alloc]init];
[self.contentView addSubview:_nightImageView1];
_nightImageView2 = [[UIImageView alloc]init];
[self.contentView addSubview:_nightImageView2];
_nightValueLabel = [[UILabel alloc]init];
[self.contentView addSubview:_nightValueLabel];
_daytimeValueLabel = [[UILabel alloc]init];
[self.contentView addSubview:_daytimeValueLabel];
_unitLabel = [[UILabel alloc]init];
[self.contentView addSubview:_unitLabel];
self.checkBoxBtn = [[QWCheckBox alloc]init];
self.checkBoxBtn.titleLabel.text = @"显示";
self.checkBoxBtn.titleLabel.textColor = [UIColor blackColor];
[self.contentView addSubview:self.checkBoxBtn];
}
return self;
}
/**
* 根据view的tag来执行不同cell风格
*
*/
-(void)updateCellSubviewsFrame:(CGRect)rect data:(QWPlanInfo *)data {
self.frame = rect;
if (data){
switch (data.type) {
case 1:
[self updateCellSubviewsFrameOnType1:rect data:data];
break;
case 2:
[self updateCellSubviewsFrameOnType2:rect data:data];
break;
default:
break;
}
}
}
/**
* cell类型一 风格(颜色/名称/数值/单位/复选框)
*
* @param rect 大小
* @param data 数据
*/
-(void)updateCellSubviewsFrameOnType1:(CGRect)rect data:(QWPlanInfo *)data {
CGFloat startX = 0;
CGFloat startY = 0;
CGFloat usableSpace = rect.size.width - self.margin*2;
//设置颜色label
CGFloat width = self.firstViewWidth;
CGFloat height = 10;
startX = self.margin;
startY = self.margin+(rect.size.height - height - self.margin*2)/2;
_data = data;
_colorLabel.frame = CGRectMake(startX, startY,width, height);
_colorLabel.backgroundColor = [QWColorTransfer colorWithHexString:data.color];
usableSpace -= _colorLabel.frame.size.width;
//设置复选按钮
startX = rect.size.width - self.lastViewWidth - self.margin;
height = rect.size.height - self.margin*2;
startY = self.margin+(rect.size.height - height - self.margin*2)/2;
self.checkBoxBtn.frame = CGRectMake(startX, startY, self.lastViewWidth,self.lastViewWidth);
usableSpace -= self.checkBoxBtn.frame.size.width - self.margin*2;
width = usableSpace/2.5;
//设置名称
_nameLabel.frame = [self getNextViewFrame:_colorLabel width:width height:height];
_nameLabel.text = data.name;
//设置值
_valueLabel.frame = [self getNextViewFrame:_nameLabel width:width/2 height:height];
_valueLabel.text = data.value;
_valueLabel.textColor = [UIColor blueColor];
//设置单位
startX = _valueLabel.frame.origin.x + _valueLabel.frame.size.width;
startY = self.margin + (self.frame.size.height - height - self.margin*2)/2;
_unitLabel.frame = CGRectMake(startX, startY, width/2, height);
_unitLabel.text = data.unit;
}
/**
* cell类型二 风格 (已走未走图标及比值/复选框)
*
* @param rect 大小
* @param data 数据
*/
-(void)updateCellSubviewsFrameOnType2:(CGRect)rect data:(QWPlanInfo *)data {
static CGFloat startX = 0;
CGFloat startY = 0;
CGFloat usableSpace = rect.size.width - self.margin*2;
//设置白天两张图片
CGFloat width = 20;
CGFloat height = 20;
startX = self.margin;
startY = self.margin+(rect.size.height - height - self.margin*2)/2;
_data = data;
_dayImageView1.frame = CGRectMake(startX, startY,width, height);
_dayImageView1.image = [UIImage imageNamed:data.daytimeTrackedImage1];
startX = _dayImageView1.frame.origin.x + _dayImageView1.frame.size.width+self.margin/2;
_dayImageView2.frame = CGRectMake(startX, startY,width, height);
_dayImageView2.image = [UIImage imageNamed:data.daytimeTrackedImage2];
//设置复选按钮
startX = rect.size.width - self.lastViewWidth - self.margin;
height = rect.size.height - self.margin*2;
startY = self.margin+(rect.size.height - height - self.margin*2)/2;
self.checkBoxBtn.frame = CGRectMake(startX, startY, self.lastViewWidth, self.lastViewWidth);
usableSpace -= (self.checkBoxBtn.frame.size.width)/2 ;
//设置白天已走与未走的比值
width = usableSpace - _dayImageView2.frame.size.width - self.margin;
startX = _dayImageView2.frame.origin.x + _dayImageView2.frame.size.width + self.margin;
_daytimeValueLabel.frame = CGRectMake(startX, startY, width, height);
_daytimeValueLabel.text = data.daytimeTrackedValue;
_daytimeValueLabel.textColor = [UIColor blueColor];
//设置夜晚两张图片
_nightImageView1.image = [UIImage imageNamed:data.nightTrackedImage1];
_nightImageView2.image = [UIImage imageNamed:data.nightTrackedImage2];
startX = usableSpace/2 + self.margin;
_nightImageView1.frame = CGRectMake(startX, startY, _dayImageView2.frame.size.width, _dayImageView2.frame.size.height);
_nightImageView2.frame = CGRectMake(startX+_nightImageView1.frame.size.width+self.margin/2, startY, _dayImageView2.frame.size.width, _dayImageView2.frame.size.height);
//设置夜晚已走与未走的比值
startX =_nightImageView2.frame.origin.x + _nightImageView2.frame.size.width + self.margin;
width = usableSpace - _nightImageView2.frame.size.width*2 - self.margin*2;
_nightValueLabel.frame = CGRectMake(startX, startY, width, height);
_nightValueLabel.text = data.nightTrackedValue;
_nightValueLabel.textColor = [UIColor blueColor];
}
/**
* 得到下一视图的大小
*
* @param view 视图类型
* @param width 宽度
* @param height 高度
*
* @return rect大小
*/
-(CGRect)getNextViewFrame:(UIView*)view width:(CGFloat)width height:(CGFloat)height{
if(!view){
return CGRectZero;
}
CGFloat ox = 0;
CGFloat oy = 0;
ox = view.frame.origin.x+view.frame.size.width+ self.margin*2;
oy = self.margin + (self.frame.size.height - height - self.margin*2)/2;
return CGRectMake(ox, oy, width, height);
}
/**
* 点击checkBox触发事件
*/
-(void)checkBoxClickAction{
if(self.delegate){
[self.delegate planInfoCell:self stateChageForInfo:_data];
}
}
// QWPlanInfoTableView.m
// QWPlanInfo
//
// Created by SevenJohs on 16/4/22.
// Copyright © 2016年 ecity. All rights reserved.
//
#import "QWPlanInfoTableView.h"
#import "QWPlanInfoCell.h"
#define FIRSTITEM_WIDTH 40
#define LASTITEM_WIDTH 25
#define ITEM_MARGIN 5
@interface QWPlanInfoTableView ()<UITableViewDelegate,UITableViewDataSource,PlanInfoDelegate>
@property (nonatomic,strong)NSArray<QWPlanInfo *>* menuGroupsArr;
@end
@implementation QWPlanInfoTableView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self menuGroupsArr];
_planInfoTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, frame.size.height + 35)];
_planInfoTableView.dataSource = self;
_planInfoTableView.delegate = self;
_planInfoTableView.separatorStyle = NO;
//[_planInfoTableView setAllowsSelection:NO];
[_planInfoTableView registerClass:[QWPlanInfoCell class] forCellReuseIdentifier:@"cell"];
[self addSubview:_planInfoTableView];
}
return self;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.menuGroupsArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* idet = @"cell";
QWPlanInfoCell* cell = (QWPlanInfoCell *)[tableView dequeueReusableCellWithIdentifier:idet];
if(cell == nil){
cell = [[QWPlanInfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idet];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
cell.firstViewWidth = FIRSTITEM_WIDTH;
cell.lastViewWidth = LASTITEM_WIDTH;
cell.margin = ITEM_MARGIN;
cell.delegate =self;
CGRect rect = cell.frame;
rect.size.width = tableView.frame.size.width;
rect.size.height = [self tableView:tableView heightForRowAtIndexPath:indexPath];
QWPlanInfo* menuModel = [self.menuGroupsArr objectAtIndex:indexPath.row];
[cell updateCellSubviewsFrame:rect data:menuModel];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 30;
}
#pragma mark -懒加载
-(NSArray*)menuGroupsArr{
if (_menuGroupsArr == nil) {
NSString* fullPath = [[NSBundle mainBundle]pathForResource:@"detailMenu" ofType:@"plist"];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath];
NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count];
for (NSDictionary *dict in dictArray) {
QWPlanInfo *menuModel = [QWPlanInfo menuWithDic:dict];
[models addObject:menuModel];
}
return models;
}
return _menuGroupsArr;
}
-(BOOL)prefersStatusBarHidden{
return YES;
}
-(void)planInfoCell:(QWPlanInfoCell *)cell stateChangedForInfo:(QWPlanInfo *)data{
}
/**
* 设置cell中的点击操作、值变化委托方法实现
*
* @param cell 指定cell
* @param data 数据更新
*/
-(void)planInfoCell:(QWPlanInfoCell *)cell stateChageForInfo:(QWPlanInfo *)data{
if (cell.checkBoxBtn.selected) {
cell.checkBoxBtn.selected = YES;
}
cell.checkBoxBtn.selected = NO;
}
@end
// Copyright © 2016年 ecity. All rights reserved.
//
#import "QWPlanInfoController.h"
#import "QWPlanInfoTableView.h"
#define EDGE_OFFSET 10
@interface QWPlanInfoController ()
@property (nonatomic,strong)QWPlanInfoTableView* menuTableView;
@property (nonatomic,strong)NSArray* menuGroups;
@end
@implementation QWPlanInfoController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [self setRightBarbuttonItem];
self.menuTableView = [[QWPlanInfoTableView alloc]initWithFrame:CGRectMake(EDGE_OFFSET, 0, self.view.frame.size.width - EDGE_OFFSET*2,150)];
// [self.menuTableView.planInfoTableView addTarget:self action:@selector(checkBoxBtnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.menuTableView];
}
-(UIBarButtonItem *)setRightBarbuttonItem{
UIBarButtonItem* rightBarItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(tableViewShows)];
return rightBarItem;
}
//-(void)checkBoxBtnClick{
//
// if (self.cell.checkBoxBtn.selected) {
// self.cell.checkBoxBtn.selected = NO;
// }
// self.cell.checkBoxBtn.selected = YES;
//}
-(void)tableViewShows{
if(self.menuTableView.isHidden){
[self.menuTableView setHidden:NO];
[self.menuTableView.planInfoTableView reloadData];
// [_allCheckBox setSelected:YES];
} else {
[self.menuTableView setHidden:YES];
// [_allCheckBox setSelected:NO];
}
}
end .