码迷,mamicode.com
首页 > 其他好文 > 详细

源码-0203-06-自定义等高cell05-代码-Autolayout

时间:2017-02-24 15:42:30      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:uilabel   orange   self   logs   code   efi   short   lis   模型   

 

//
//  XMGDealsViewController.m
//  06-自定义等高cell01-storyboard
#import "XMGDealsViewController.h"
#import "XMGDeal.h"
#import "XMGDealCell.h"

@interface XMGDealsViewController ()
/** 所有的团购数据 */
@property (nonatomic, strong) NSArray *deals;
@end

@implementation XMGDealsViewController

- (NSArray *)deals
{
    if (_deals == nil) {
        // 加载plist中的字典数组
        NSString *path = [[NSBundle mainBundle] pathForResource:@"deals.plist" ofType:nil];
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        // 字典数组 -> 模型数组
        NSMutableArray *dealArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            XMGDeal *deal = [XMGDeal dealWithDict:dict];
            [dealArray addObject:deal];
        }
        
        _deals = dealArray;
    }
    return _deals;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
//    UINib *nib = [UINib nibWithNibName:NSStringFromClass([XMGDealCell class]) bundle:nil];
//    [self.tableView registerNib:nib forCellReuseIdentifier:@"deal"];
    
    [self.tableView registerClass:[XMGDealCell class] forCellReuseIdentifier:@"deal"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.deals.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //创建cell
    XMGDealCell *cell = [XMGDealCell cellWithTableView:tableView];
    // 取出模型数据
    cell.deal = self.deals[indexPath.row];
    
    return cell;
}

@end

 

//
//  XMGDealCell.h
//  06-自定义等高cell01-storyboard
#import <UIKit/UIKit.h>
@class XMGDeal;

@interface XMGDealCell : UITableViewCell
/** 团购模型数据 */
@property (nonatomic, strong) XMGDeal *deal;

+ (instancetype)cellWithTableView:(UITableView *)tableView;
@end
//
//  XMGDealCell.m
//  06-自定义等高cell01-storyboard
#import "XMGDealCell.h"
#import "XMGDeal.h"
//define this constant if you want to use Masonry without the ‘mas_‘ prefix
#define MAS_SHORTHAND
//define this constant if you want to enable auto-boxing for default syntax
#define MAS_SHORTHAND_GLOBALS
#import "Masonry.h"

@interface XMGDealCell()
@property (weak, nonatomic) UIImageView *iconView;
@property (weak, nonatomic) UILabel *titleLabel;
@property (weak, nonatomic) UILabel *priceLabel;
@property (weak, nonatomic) UILabel *buyCountLabel;
@end

@implementation XMGDealCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"deal";
    // 创建cell
    XMGDealCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[XMGDealCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

// 1.在initWithStyle:reuseIdentifier:方法中添加子控件
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        CGFloat margin = 10;
        
        UIImageView *iconView = [[UIImageView alloc] init];
        [self.contentView addSubview:iconView];
        self.iconView = iconView;
        [iconView makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(100);
            make.left.top.offset(margin);
            make.bottom.offset(-margin);
        }];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:titleLabel];
        self.titleLabel = titleLabel;
        [titleLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(iconView);
            make.left.equalTo(iconView.right).offset(margin);
            make.right.offset(-margin);
        }];
        
        UILabel *priceLabel = [[UILabel alloc] init];
        priceLabel.textColor = [UIColor orangeColor];
        [self.contentView addSubview:priceLabel];
        self.priceLabel = priceLabel;
        [priceLabel makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLabel);
            make.bottom.equalTo(iconView);
            make.width.equalTo(70);
        }];
        
        UILabel *buyCountLabel = [[UILabel alloc] init];
        buyCountLabel.textAlignment = NSTextAlignmentRight;
        buyCountLabel.font = [UIFont systemFontOfSize:14];
        buyCountLabel.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:buyCountLabel];
        self.buyCountLabel = buyCountLabel;
        [buyCountLabel makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(priceLabel);
            make.right.equalTo(titleLabel);
            make.left.equalTo(priceLabel.right).offset(margin);
        }];
    }
    return self;
}

// 3.重写模型的set方法
- (void)setDeal:(XMGDeal *)deal
{
    _deal = deal;
    
    // 设置数据
    self.iconView.image = [UIImage imageNamed:deal.icon];
    self.titleLabel.text = deal.title;
    self.priceLabel.text = [NSString stringWithFormat:@"¥%@", deal.price];
    self.buyCountLabel.text = [NSString stringWithFormat:@"%@人已购买", deal.buyCount];
}

@end

 

//
//  XMGDeal.h
//  06-自定义等高cell01-storyboard
#import <Foundation/Foundation.h>

@interface XMGDeal : NSObject
@property (strong, nonatomic) NSString *buyCount;
@property (strong, nonatomic) NSString *price;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *icon;

+ (instancetype)dealWithDict:(NSDictionary *)dict;
@end
//
//  XMGDeal.m
//  06-自定义等高cell01-storyboard
#import "XMGDeal.h"

@implementation XMGDeal
+ (instancetype)dealWithDict:(NSDictionary *)dict
{
    XMGDeal *deal = [[self alloc] init];
    
//    deal.title = dict[@"title"];
//    deal.icon = dict[@"icon"];
//    deal.buyCount = dict[@"buyCount"];
//    deal.price = dict[@"price"];
    
    // KVC - Key Value Coding
    [deal setValuesForKeysWithDictionary:dict];
    
    return deal;
}
@end

 

源码-0203-06-自定义等高cell05-代码-Autolayout

标签:uilabel   orange   self   logs   code   efi   short   lis   模型   

原文地址:http://www.cnblogs.com/laugh/p/6438549.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!