一、新建xib描述cell的样子—SystemStatusCell.xib
File—new file—ios—user interface—Empty,命名为SystemStatusCell.xib;
修改xib文件中cell的类名为SystemStatusCell,如图上红框标出的地方所示;
二、封装这个cell
File—new file—ios—source—Cocoa Touch Class,其class命名为SystemStatusCell,继承于(subclass of)UITableViewCell;
就会多出两个文件SystemStatusCell.h和SystemStatusCell.m;
三、将xib中所有子控件封装在cell内部
方法1、给所有子控件绑定一个tag;
方法2、连线;
重点讲这个方法2,
(1)先分别打开两个文件:SystemStatusCell.xib文件和SystemStatusCell.h文件:
(2)如图所示,我已经连线好了,只是演示怎么做的,按住ctrl键,鼠标左键点击icon View,拖到右边文件空白行,如图操作:
(3)然后会出现这样的框框,输入相关属性值:
(4)其他两个控件同上;
四、新建模型SystemStatus,封装数据
File—new file—ios—user interface—Empty,命名为SystemStatus,继承于NSOnject,多出两个文件:SystemStatus.h和SystemStatus.m;
//
// SystemStatus.h
// 模型类,不用字典plist开发
// DATAEYE
//
// Created by SmartEnergy on 16/9/26.
// Copyright © 2016年 SmartEnergy. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface SystemStatus : NSObject
@property (copy, nonatomic) NSString *icon; // 图片
@property (copy, nonatomic) NSString *title; // 标题
@property (assign, nonatomic) double number; // 数量,double类型用assign
- (id)initWithDict:(NSDictionary *)dict;
+ (id)SystemStatusWithDict:(NSDictionary *)dict;
@end
五、 给SystemStatusCell增加模型属性
子控件的显示什么数据取决于模型里的每一个属性,
就是加粗地方的代码:
//
// SystemStatusCell.h
// 一个类,封装了cell
// DATAEYE
//
// Created by SmartEnergy on 16/9/26.
// Copyright © 2016年 SmartEnergy. All rights reserved.
//
#import <UIKit/UIKit.h>
@class SystemStatus; // 传SystemStatus模型
@interface SystemStatusCell : UITableViewCell
// cell通过连线的方法访问内部的子控件
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *numberLabel;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
**@property (strong, nonatomic) SystemStatus *systemStatus;// 系统状态模型数据,对象要用strong**
+ (id)systemStatusCell;
+ (NSString *)ID;
@end
六、 重写set方法,在这个方法中,根据模型数据设置cell内部子控件的属性
//
// SystemStatusCell.m
// DATAEYE
//
// Created by SmartEnergy on 16/9/26.
// Copyright © 2016年 SmartEnergy. All rights reserved.
//
#import "SystemStatusCell.h"
#import "SystemStatus.h"
@implementation SystemStatusCell
**+ (id)systemStatusCell {
return [[NSBundle mainBundle] loadNibNamed:@"SystemStatusCell" owner:nil options:nil][0];
}**
// 重写set方法,set模型,根据模型属性设置内部子控件的属性
- (void)setSystemStatus:(SystemStatus *)systemStatus {
**// 给成员变量赋值
_systemStatus = systemStatus;
// 1.标题
_titleLabel.text = systemStatus.title;
// 2.数据
_numberLabel.text = [NSString stringWithFormat:@"数据:%f",systemStatus.number];
// 3.图片
_iconView.image = [UIImage imageNamed:systemStatus.icon];**
}
+ (NSString *)ID {
return @"Cell";// 控制器拿到的标识是通过viewcontroller中的cellForRowAtIndexPath传过来的
}
@end
七、 提供一个类方法,返回从xib中创建好的cell对象
即步骤六中的代码,我再写一遍,哈哈哈:
+ (id)systemStatusCell {
return [[NSBundle mainBundle] loadNibNamed:@"SystemStatusCell" owner:nil options:nil][0];
}
八、 给xib中的cell添加一个重用标识(比如:Cell),提供一个类方法,返回重用标识
+ (NSString *)ID {
return @"Cell";// 控制器拿到的标识是通过viewcontroller中的cellForRowAtIndexPath传过来的
}
从哪里读取数据,是控制器要做的事情;
cell只管显示数据;
//
// SystemStatusViewController.m
// DATAEYE
//
// Created by SmartEnergy on 16/9/26.
// Copyright © 2016年 SmartEnergy. All rights reserved.
//
#import "SystemStatusViewController.h"
#import "SystemStatus.h"
#import "SystemStatusCell.h"
@interface SystemStatusViewController () {
// NSMutableArray *_systemStatuss;// 成员变量数组,可变的,所有的系统状态
NSArray *_system;
}
@end
@implementation SystemStatusViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 所有cell高度一致的时候,可使用这行代码,否则使用heightForRowAtIndexPath
self.tableView.rowHeight = 80;
// _systemStatuss = [NSMutableArray array];;
SystemStatus *s1 = [[SystemStatus alloc]init];
s1.icon = @"image1.gif";
s1.title = @"正常运行";
s1.number = 1;
SystemStatus *s2 = [[SystemStatus alloc]init];
s2.icon = @"image2.gif";
s2.title = @"错误设备";
s2.number = 2;
SystemStatus *s3 = [[SystemStatus alloc]init];
s3.icon = @"image3.gif";
s3.title = @"连接超时";
s3.number = 3;
SystemStatus *s4 = [[SystemStatus alloc]init];
s4.icon = @"image4.png";
s4.title = @"输出功率(W)";
s4.number = 4;
SystemStatus *s5 = [[SystemStatus alloc]init];
s5.icon = @"image5.png";
s5.title = @"日发电量(KWh)";
s5.number = 5;
SystemStatus *s6 = [[SystemStatus alloc]init];
s6.icon = @"image6.png";
s6.title = @"月发电量(KWh)";
s6.number = 6.666;
SystemStatus *s7 = [[SystemStatus alloc]init];
s7.icon = @"image7.png";
s7.title = @"总发电量(KWh)";
s7.number = 7.70;
_system = @[s1,s2,s3,s4,s5,s6,s7];
/*
// 加载plist数据
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"SystemStatus.plist" ofType:nil]];
// 字典转模型table
_systemStatuss = [NSMutableArray array];
// 遍历array里面所有的系统状态字典
for (NSDictionary *dict in array) {
// 创建一个系统状态,并添加到视图中去
[_systemStatuss addObject:[SystemStatus SystemStatusWithDict:dict]];
}
*/
}
//- (void)didReceiveMemoryWarning {
// [super didReceiveMemoryWarning];
// // Dispose of any resources that can be recreated.
//}
#pragma mark - Table view data source
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// return 0;
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _system.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SystemStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:[SystemStatusCell ID]];
if (cell == nil) {
cell = [SystemStatusCell systemStatusCell];
}
cell.systemStatus = _system[indexPath.row];
return cell;
/*
// 0.定义循环标记
// static NSString *CellIdentifier = @"systemstatus";
// static NSString *CellIdentifier = @"Cell";
// 1.从缓存池中取出cell
// SystemStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// SystemStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
SystemStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:[SystemStatusCell ID]];// cell来告诉我们id是多少
// 2.缓存池中没有cell
if (cell == nil) {
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// 加载xib文件的两种方式
// 方法1(SystemStatusCell是xib文件的名称)
// NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"SystemStatusCell" owner:nil options:nil];
// 方法2
// UINib *nib = [UINib nibWithNibName:@"SystemStatusCell" bundle:nil]; // 传的nil就为mainBundle
// NSArray *objects = [nib instantiateWithOwner:nil options:nil];
// cell = objects[0];
cell = [SystemStatusCell systemStatusCell];
}
// 3.取出系统状态模型
// SystemStatus *systemStatus = _systemStatuss[indexPath.row];
// 4.传递模型数据
// cell.systemStatus = systemStatus;
cell.systemStatus = _systemStatuss[indexPath.row];
// cell.systemStatus = _systemStatuss[indexPath.row];
// [cell setSystemStatus:systemStatus];
// 4.系统状态标题
// UILabel *titleLabel = (UILabel *)[cell viewWithTag:2];
// titleLabel.text = systemStatus.title;
// 5.图片
// UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
// imageView.image = [UIImage imageNamed:systemStatus.icon];
// NSLog(@"%p",cell);
return cell;
*/
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
#pragma mark - 通过代理方法返回cell高度(cell高度不一致的时候可以使用-根据传的行号,返回不同的高度)
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// return 80;
//}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// SystemStatus.m
// 模型类,不用字典plist开发
// DATAEYE
//
// Created by SmartEnergy on 16/9/26.
// Copyright © 2016年 SmartEnergy. All rights reserved.
//
#import "SystemStatus.h"
@implementation SystemStatus
- (id)initWithDict:(NSDictionary *)dict {
if (self = [super init]) {
// 解析字典属性(转化为模型属性)
self.title = dict[@"title"];
self.icon = dict[@"icon"];
self.number = [dict[@"number"] doubleValue];// 是number类型的,转为double类型
}
return self;
}
+ (id)SystemStatusWithDict:(NSDictionary *)dict {
return [[self alloc] initWithDict:dict]; // 传字典来构造对象
}
@end
//
// SystemStatusViewController.h
// DATAEYE
//
// Created by SmartEnergy on 16/9/26.
// Copyright © 2016年 SmartEnergy. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface SystemStatusViewController : UITableViewController
@end