// // TableheaderView.m // Haishenghai-master // // Created by GG on 2019/1/5. // Copyright © 2019年 Haishenghai intelligence network technology. All rights reserved. // #import "TableheaderView.h" #define ScreenWidth [UIScreen mainScreen].bounds.size.width #define ScreenHeight [UIScreen mainScreen].bounds.size.height @implementation TableheaderView -(instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { _nameLabel = [[UILabel alloc]init]; // _nameLabel.text = @"独立式烟雾探测报警器"; _nameLabel.textAlignment = NSTextAlignmentLeft; _nameLabel.font = [UIFont systemFontOfSize:16]; [self addSubview:_nameLabel]; _stateLabel = [[UILabel alloc]init]; // _stateLabel.text = @"正常"; _stateLabel.font = [UIFont systemFontOfSize:16]; _stateLabel.textColor = [UIColor blueColor]; [self addSubview:_stateLabel]; _image =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"hsh_home_equipmentdetails"]]; [self addSubview:_image]; //四个标题 for (int i=0; i<4; i++) { UILabel *titlelab= [[UILabel alloc]init]; titlelab.frame = CGRectMake(70+(75)*(i/4), 30+(20)*(i%4), 75, 20); NSArray *titles = @[@"运 营 商:",@"编 号:",@"位 置:",@"详细地址:"]; titlelab.text = titles[i]; titlelab.font = [UIFont systemFontOfSize:14]; [self addSubview:titlelab]; } //四个内容 for (int i=0; i<2; i++) { UILabel *nameLabel= [[UILabel alloc]init]; nameLabel.frame = CGRectMake((ScreenWidth-100)+(75)*(i/2), 30+(20)*(i%2), 75, 20); NSArray *titles = @[@"电池电压:",@"网关状态:"]; nameLabel.text = titles[i]; nameLabel.font = [UIFont systemFontOfSize:14]; [self addSubview:nameLabel]; } _recordLabel= [[UILabel alloc]init]; _recordLabel.frame = CGRectMake((ScreenWidth-35), 30, 35, 20); _recordLabel.text = @"3.0V"; _recordLabel.font = [UIFont systemFontOfSize:14]; [self addSubview:_recordLabel]; _gatewayLabel= [[UILabel alloc]init]; _gatewayLabel.frame = CGRectMake((ScreenWidth-35), 50, 35, 20); _gatewayLabel.text = @"离线"; _gatewayLabel.font = [UIFont systemFontOfSize:14]; [self addSubview:_gatewayLabel]; //第一列四个内容 _detailLabel = [[UILabel alloc]init]; _detailLabel.frame = CGRectMake(140, 30,125 , 20); _detailLabel.text = @"电信NB-IOT"; // _detailLabel.backgroundColor = [UIColor purpleColor]; _detailLabel.font = [UIFont systemFontOfSize:14]; [self addSubview:_detailLabel]; _numberLabel = [[UILabel alloc]init]; _numberLabel.frame = CGRectMake(140, 50,140 , 20); // _numberLabel.text = @"编号"; _numberLabel.font = [UIFont systemFontOfSize:14]; [self addSubview:_numberLabel]; _locationLabel = [[UILabel alloc]init]; _locationLabel.frame = CGRectMake(140, 70,200 , 20); // _locationLabel.text = @"位置"; _locationLabel.font = [UIFont systemFontOfSize:14]; [self addSubview:_locationLabel]; _d_locationLabel = [[UILabel alloc]init]; _d_locationLabel.frame = CGRectMake(140, 90,200 , 20); // _d_locationLabel.text = @"位置"; _d_locationLabel.font = [UIFont systemFontOfSize:14]; [self addSubview:_d_locationLabel]; UIView *lineView = [[UIView alloc]init]; lineView.frame = CGRectMake(0, 110, ScreenWidth, 0.5); lineView.backgroundColor = [UIColor lightGrayColor]; [self addSubview:lineView]; } return self; } //在这里添加frame和约束 -(void)layoutSubviews{ _nameLabel.frame = CGRectMake(10, 5, ScreenWidth-100, 25); _stateLabel.frame = CGRectMake(ScreenWidth-40, 5, 40, 25); _image.frame = CGRectMake(0, 30, 70, 70); } //加载数据 //- (void)setTopic: (SYDTopic *)topic //{ // _topic = topic; // 注意在这个方法中,不写这句也是没有问题的,因为在下面的语句使用的是topic而非self.topic或_topic,但是如果在其他的方法中也想要访问topic这个属性,那么就需要写上,否则self.topic或_topic会一直是nil(因为出了这个方法的作用域,topic就销毁了,如果再想访问需要有其他的引用指向它)。所以建议,要写上这句。 // // [self.button setTitle: topic.like forState...]; // self.nameLabel = topic.name; //} /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ /** *总结 initWithFrame:中添加子控件。 layoutSubviews中设置子控件frame。 对外设置数据接口,重写setter方法给子控件设置显示数据。 在view controller里面使用init/initWithFrame:方法创建自定义类,并且给自定义类的frame赋值。 对自定义类对外暴露的数据接口进行赋值即可。 * */ @end