封装
Girl.h++++++++++++++++++++++++++++++++++++++++++++++++++++
name,image
// 提供一个类方法,即构造函数,返回封装好数据的对象(返回id亦可)
+ (Girl *)girlNamed:(NSString *)name headImgName:(NSString*)headImgName verdict:(NSString *)verdict;
{ Girl *girl = [[Girl alloc]init];
girl.name = name;
return girl;
}
// 类方法,字典 转 对象 类似javaBean一次性填充
+ (Girl *)girlWithDict:(NSDictionary *)dict;
// 只是调用对象的initWithDict方法,之所以用self是为了对子类进行兼容
{ return [[self alloc]initWithDict:dict]; }
// 对象方法,设置对象的属性后,返回对象
- (Girl *)initWithDict:(NSDictionary *)dict;
{
// 先调用父类NSObject的init方法
if (self = [super init]) {
// 设置对象自己的属性
self.name = dict[@"name"];
}
// 返回填充好的对象
return self;
}
cell++++++++++++++++++++++++++++++++++++++++++++++++++
@class Girl
@property (weak, nonatomic) IBOutlet UILabel *verdict;
// 返回xib界面上写的重用cellID
+(NSString *)cellID
{
// 必须和界面上的一致
return @"GirlCell";
}
// 从xib中加载 实例化一个girlCell对象
+ (GirlCell *)girlCell
{
// mainBundel加载xib,扩展名不用写.xib
NSArray *arrayXibObjects = [[NSBundle mainBundle] loadNibNamed:@"GirlCell" owner:nil options:nil];
return arrayXibObjects[0];
}
// 返回封装好数据之后的,girlCell对象
- (GirlCell *)cellWithGirl:(Girl *)girl
{
// 前面,通过连线,将xib中的各个控件,连接到GirlCell类,成为它的成员属性了,这样一来就不用通过tag取得xib中每一个控件了
_name.text = girl.name;
// 返回封装好数据之后的,girlCell对象
return self;
}
ViewController++++++++++++++++++++++++++++++++++++++++++++++++
@interface BeyondTableViewController ()
{
// 从plist文件中加载的所有girls,返回所有的对象组成的数组
NSMutableArray *_girls;
}
@end
@implementation BeyondTableViewController
// 隐藏顶部的状态栏
- (BOOL)prefersStatusBarHidden
{
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 以下三步为OC标准代码,因为OC中不允许直接修该对象中结构体属性的成员的值,要通过中间的临时结构体变量
CGRect frame = self.tableView.frame;
frame.origin.y = 20;
self.tableView.frame=frame;
_girls = [NSMutableArray array];
// plist转成对象数组
[self plistToObjects];
// self.clearsSelectionOnViewWillAppear = NO;
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
// plist转成对象数组
- (void)plistToObjects
{
// sg_bundle模板代码,1,获得.app主要的包;2,返回主要的包中某个文件的fullPath全路径
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *fullPath = [mainBundle pathForResource:@"girls.plist" ofType:nil];
// 从plist文件中根据全路径,返回字典数组
NSArray *arrayWithDict = [NSArray arrayWithContentsOfFile:fullPath];
// 字典数组 _arrayWithDict
// 方式2:类方法返回对象,参数只要一个字典数组即可
for (NSDictionary *dict in arrayWithDict) {
// 参数只要字典,这样一来,控制器就不用知道太多东西了
// Girl *girl = [[Girl alloc]initWithDict:dict];
Girl *girl = [Girl girlWithDict:dict];
// Girl *girl = [[Girl alloc]initWithDict:dict];
[_girls addObject:girl];
}
}
#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _girls.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 把cell封装之后,控制 器知道的东西就非常少了~
// 自已的cell类
GirlCell *girlCell = [tableView dequeueReusableCellWithIdentifier:[GirlCell cellID]];
if (girlCell == nil) {
// 如果池中没取到,则重新生成一个girlCell,xib文件中指定了重用cellID
girlCell = [GirlCell girlCell];
}
// 设置cell中独一无二的内容
Girl *girl = [_girls objectAtIndex:indexPath.row];
// 参数为girl对象,填充值到xib中各个控件 ,并返回填充好的cell
girlCell = [girlCell cellWithGirl:girl];
// 返回cell
return girlCell;
}
#pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return kCellHight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end