| 本帖最后由 竹林风 于 2018-12-5 18:17 编辑 
 
 文章导航   
 
 创建本地plist文件数据:
 
 
     
 创建完成结构如下:
 
 
   
 创建数据模型
 
 
     
 FruitModel.h
 
 
 [Objective-C] 纯文本查看 复制代码 #import <Foundation/Foundation.h>
@interface FruitModel : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *egname;
@property (nonatomic, strong) NSString *icon;
@property (nonatomic, strong) NSString *desc;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)fruitWithDict:(NSDictionary *)dict;
@end
 FruitModel.m
 
 
 [Objective-C] 纯文本查看 复制代码 #import "FruitModel.h"
@implementation FruitModel
-(instancetype)initWithDict:(NSDictionary *)dict{
    
    if (self == [super init]) {
        self.name = dict[@"name"];
        self.egname = dict[@"egname"];
        self.icon = dict[@"icon"];
        self.desc = dict[@"desc"];
    }
    return self;
}
+ (instancetype)fruitWithDict:(NSDictionary *)dict{
    
    return [[self alloc] initWithDict:dict];
}
@end
 读取本地数据文件并加载数据
 
 
 [Objective-C] 纯文本查看 复制代码 #import "firstVC.h"
#import "FruitModel.h"
@interface firstVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *mAryFruit;
@end
@implementation firstVC
-(NSMutableArray *)mAryFruit{
    if (_mAryFruit == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"Fruit.plist" ofType:nil];
        NSArray *arrayDict = [NSArray arrayWithContentsOfFile:path];
        //字典转模型
        NSMutableArray *arrayModels = [NSMutableArray array];
        for (NSDictionary *dict in arrayDict) {
            FruitModel *model = [FruitModel fruitWithDict:dict];
            [arrayModels addObject:model];
        }
        _mAryFruit = arrayModels;
    }
    return _mAryFruit;
}
 用xib方式创建一个文件。在xib中拖一个UITableViewCell,设置高宽。向UITableViewCell中拖子控件。
 
 
 
     
 创建一个继承自UITableViewCell的类ProductCell与xib文件的cell相关联。通过拖线的方式将cell的子控件拖线到ProductCell的属性上。
 
 
 
   
 实现数据源协议:firstVC.m
 
 
 [Objective-C] 纯文本查看 复制代码 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}
/**
 *  告诉tableView第section组有多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _mAryFruit.count;
}
/**
 *  告诉tableView第indexPath行显示怎样的cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //1.获取模型数据
    FruitModel *model = self.mAryFruit[indexPath.row];
    //2.创建单元格
    //通过xib创建单元格
    //由于此方法调用十分频繁,cell的标示声明成静态变量有利于性能优化
    static NSString *ID = @"fruit_cell"; //要在xib中设置这个id
    FruitCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        //加载xib文件,loadNibName:方法返回的是一个数组,因为xib中可能有不止一个控件
        cell = [[[NSBundle mainBundle]loadNibNamed:@"FruitCell" owner:nil options:nil] firstObject];//不能加xib后缀
    }
    //3.把模型数据设置给单元格
    cell.lblName.text = model.name;
    cell.lblEgname.text = model.egname;
    cell.imgIcon.image = [UIImage imageNamed: model.icon];
    
    //4.返回单元格
    return cell;
}
 这里有个问题,如果在控制器中直接为cell中的每个子控件赋值数据可能会造成以下问题
 
 
 1.控制器强依赖于cell,一旦cell内部子控件发生了变化,那么控制器中的代码也得改(紧耦合)。控制器完全依赖于单元格里面的属性。
 2.cell的封装不够完整,凡是用到cell的地方,每次都要编写为cell的子控件依次赋值的语句,比如:cell.xxx = model.xxx。如果有10个控制器,每个控制器里都需要用到单元格进行赋值,如果一个单元格里有10个子控件,那么上面这样的代码就要写10次。
 
 
 所以我们可以这样:
 
 对自定义cell进行封装,把模型数据设置给单元格,形如:cell.goods = model;由cell对象内部自己来解析模型数据,并把数据设置到对应的子控件中。在cell中创建一个模型类型的属性,重写该属性的set方法,在set方法中将数据赋值给控件。
 
 
 修改后的代码:
 
 
 FruitCell.h
 
 
 [Objective-C] 纯文本查看 复制代码 #import <UIKit/UIKit.h>
#import "FruitModel.h"
@interface FruitCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *imgIcon;
@property (weak, nonatomic) IBOutlet UILabel *lblName;
@property (weak, nonatomic) IBOutlet UILabel *lblEgname;
@property (nonatomic, strong) FruitModel *fruitModel;
//封装一个创建自定义cell的方法
+ (instancetype)FruitCellWithTableView:(UITableView *)tableView;
 FruitCell.m
 
 
 [Objective-C] 纯文本查看 复制代码 #import "FruitCell.h"
@implementation FruitCell
//重写set方法
-(void)setFruitModel:(FruitModel *)fruitModel{
    
    _fruitModel = fruitModel;
    
    self.lblName.text = fruitModel.name;
    self.lblEgname.text = fruitModel.egname;
    self.imgIcon.image = [UIImage imageNamed: fruitModel.icon];
}
+ (instancetype)FruitCellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"fruit_cell";
    FruitCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"FruitCell" owner:nil options:nil] firstObject];//不能加xib后缀
    }
    return cell;
}
@end
 修改后的数据源方法
 
 
   
 运行效果
 
   
 附件:
  Test-xib自定义cell.zip
(164.89 KB, 下载次数: 3) |