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


    //
    //  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
    本人无商业用途,仅仅是学习做个笔记,特别鸣谢小马哥,学习了IOS,另日语学习内容有需要文本和音频请关注公众号:riyuxuexishuji
  • 相关阅读:
    How can i use iptables on centos 7?
    Running Jenkins behind Nginx
    GPG入门教程
    HTML5 canvas标签-4 灰度直方图的实现
    [转载]手把手用C++解密Chrome80版本数据库
    Delphi
    7-zip Delphi API
    cef对本地web资源打包加密
    CEF3资源重定向、读取加密资源、读取zip资源
    axios设置withCredentials导致“跨域”的解决方案
  • 原文地址:https://www.cnblogs.com/laugh/p/6438549.html
Copyright © 2020-2023  润新知