IOS 优秀的封装结构-定义自己的UITableViewCell

视图控制器头文件 #WLViewController.h

#import <UIKit/UIKit.h>

@interface WLViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSArray *tuanModel;

@end


视图控制器实现 #WLViewController.m

#import "WLViewController.h"
#import "WLTuanModel.h"
#import "WLTableViewCellTuan.h"

@interface WLViewController ()<UITableViewDataSource>

@end

@implementation WLViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.dataSource=self;
    self.tableView.rowHeight=80;
}

-(NSArray *)tuanModel{
    if(_tuanModel==nil){
        //用来保存model 对象的数组
        NSMutableArray *tuans=[[NSMutableArray alloc]init];
        
        NSArray *data=[WLTuanModel tuanLoadDataByFile];
        for (NSDictionary *dict in data) {
            WLTuanModel *tuan=[WLTuanModel tuanWithDict:dict];
            [tuans addObject:tuan];
        }
        _tuanModel=tuans;
    }
    return _tuanModel;
}

/*interface DataSource
 */
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

/*interface DataSource
 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.tuanModel.count;
}

/*interface DataSource
 */
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //创建cell
    WLTableViewCellTuan * cell=[WLTableViewCellTuan cellWithTableView:tableView];
    
    //模型给cell分配数据
    cell.tuanModel=self.tuanModel[indexPath.row];
    return cell;
}
数据模型头文件 #WLTuanModel.h

#import <Foundation/Foundation.h>

@interface WLTuanModel : NSObject

@property(nonatomic,copy)NSString *icon;
@property(nonatomic,copy)NSString *price;
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *buyCount;

-(instancetype)initWithDict:(NSDictionary *)data;
+(instancetype)tuanWithDict:(NSDictionary *)data;

+(NSArray *)tuanLoadDataByFile;
@end
数据模型的实现 #WLTuanModel.m

#import "WLTuanModel.h"

@implementation WLTuanModel

-(instancetype)initWithDict:(NSDictionary *)dict{
    if(self=[super init]){
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

+(instancetype)tuanWithDict:(NSDictionary *)dict{
    return [[self alloc]initWithDict:dict];
}

+(NSArray *)tuanLoadDataByFile{
    NSString *path=[[NSBundle mainBundle]pathForResource:@"tgs" ofType:@"plist"];
    NSArray *tuans=[NSArray arrayWithContentsOfFile:path];
    return tuans;
}

@end


自定义cell类的头文件  #WLTableViewCellTuan.h

#import <UIKit/UIKit.h>
#import "WLTuanModel.h"

@interface WLTableViewCellTuan : UITableViewCell

@property(nonatomic,strong) WLTuanModel * tuanModel;

- (void)setTuanModel:(WLTuanModel *)tuanModel;

//将创建cell的代码封装到 类的内部
+(instancetype )cellWithTableView:(UITableView *)tableView;
@end

自定义cell类的实现  #WLTableViewCellTuan.m
#import "WLTableViewCellTuan.h"
#import "WLTuanModel.h"

@interface WLTableViewCellTuan()

@property (weak, nonatomic) IBOutlet UIImageView *icon;
@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UILabel *price;
@property (weak, nonatomic) IBOutlet UILabel *num;

@end

@implementation WLTableViewCellTuan

+(instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *ID=@"tuangou";
    
    WLTableViewCellTuan *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    if(cell==nil){
        cell=[[[NSBundle mainBundle]loadNibNamed:@"WLTuanCell" owner:nil options:nil] lastObject];
    }
    return cell;
}

/*通过模型设置组件数据
 */
- (void)setTuanModel:(WLTuanModel *)tuanModel{
    
    _tuanModel=tuanModel;
    
    self.icon.image=[UIImage imageNamed:tuanModel.icon];
    self.title.text=tuanModel.title;
    self.price.text=[NSString stringWithFormat:@"¥ %@",tuanModel.price ];
    self.num.text=[NSString stringWithFormat:@"购买数量 %@",tuanModel.buyCount ];
    
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值