zoukankan      html  css  js  c++  java
  • 源码-0203-06-自定义等高cell03-xib

    //
    //  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.xib

    //
    //  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"
    
    @interface XMGDealCell()
    @property (weak, nonatomic) IBOutlet UIImageView *iconView;
    @property (weak, nonatomic) IBOutlet UILabel *titleLabel;
    @property (weak, nonatomic) IBOutlet UILabel *priceLabel;
    @property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;
    @end
    
    @implementation XMGDealCell
    
    + (instancetype)cellWithTableView:(UITableView *)tableView
    {
        static NSString *ID = @"deal";
        XMGDealCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([XMGDealCell class]) owner:nil options:nil] lastObject];
        }
        return cell;
    }
    
    - (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

    自定义等高cell04-代码-frame

    //
    //  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 {
        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"
    
    @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]) {
            UIImageView *iconView = [[UIImageView alloc] init];
            [self.contentView addSubview:iconView];
            self.iconView = iconView;
            
            UILabel *titleLabel = [[UILabel alloc] init];
            [self.contentView addSubview:titleLabel];
            self.titleLabel = titleLabel;
            
            UILabel *priceLabel = [[UILabel alloc] init];
            priceLabel.textColor = [UIColor orangeColor];
            [self.contentView addSubview:priceLabel];
            self.priceLabel = priceLabel;
            
            UILabel *buyCountLabel = [[UILabel alloc] init];
            buyCountLabel.textAlignment = NSTextAlignmentRight;
            buyCountLabel.font = [UIFont systemFontOfSize:14];
            buyCountLabel.textColor = [UIColor lightGrayColor];
            [self.contentView addSubview:buyCountLabel];
            self.buyCountLabel = buyCountLabel;
        }
        return self;
    }
    
    // 2.在layoutSubviews方法中设置子控件的frame
    - (void)layoutSubviews
    {
        [super layoutSubviews];
        
        CGFloat contentH = self.contentView.frame.size.height;
        CGFloat contentW = self.contentView.frame.size.width;
        CGFloat margin = 10;
        
        CGFloat iconX = margin;
        CGFloat iconY = margin;
        CGFloat iconW = 100;
        CGFloat iconH = contentH - 2 * iconY;
        self.iconView.frame = CGRectMake(iconX, iconY, iconW, iconH);
        
        // titleLabel
        CGFloat titleX = CGRectGetMaxX(self.iconView.frame) + margin;
        CGFloat titleY = iconY;
        CGFloat titleW = contentW - titleX - margin;
        CGFloat titleH = 21;
        self.titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH);
    //    CGRectMake(titleX, titleY, titleW, titleH);
        
        // priceLabel
        CGFloat priceX = titleX;
        CGFloat priceH = 21;
        CGFloat priceY = contentH - margin - priceH;
        CGFloat priceW = 70;
        self.priceLabel.frame = CGRectMake(priceX, priceY, priceW, priceH);
        
        // buyCountLabel
        CGFloat buyCountH = priceH;
        CGFloat buyCountY = priceY;
        CGFloat buyCountX = CGRectGetMaxX(self.priceLabel.frame) + margin;
        CGFloat buyCountW = contentW - buyCountX - margin;
        self.buyCountLabel.frame = CGRectMake(buyCountX, buyCountY, buyCountW, buyCountH);
    }
    
    // 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
  • 相关阅读:
    自定义UINavigationBar
    UIImage 拉伸图片 resizableImageWithCapInsets
    自定义UINavigationBar上面的按钮
    tableView中deselectRowAtIndexPath的作用
    [LeetCode] Search a 2D Matrix, Solution
    [LeetCode] Longest Valid Parentheses, Solution
    我也写了一个牛顿迭代法,貌似不需要特殊处理溢出的情况 class Solution { public...
    [LeetCode] Remove Duplicates from Sorted List II, Solution
    [LeetCode] Palindrome Partitioning, Solution
    [LeetCode] Two Sum, Solution
  • 原文地址:https://www.cnblogs.com/laugh/p/6438521.html
Copyright © 2011-2022 走看看