源码-0203-自定义等高cell01-storyboard

先连网,获取数据,转成模型,通过模型将数据设置的对应的控件上; 

//
//  XMGDealsViewController.m
//  06-自定义等高cell01-storyboard
#import "XMGDealsViewController.h"
#import "XMGDeal.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];
    
    
}

- (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 {
    static NSString *ID = @"deal";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 取出模型数据
    XMGDeal *deal = self.deals[indexPath.row];
    
    // 设置数据
    UIImageView *iconView = (UIImageView *)[cell viewWithTag:10];
    iconView.image = [UIImage imageNamed:deal.icon];
    
    UILabel *titleLabel = (UILabel *)[cell viewWithTag:20];
    titleLabel.text = deal.title;
    
    UILabel *priceLabel = (UILabel *)[cell viewWithTag:30];
    priceLabel.text = [NSString stringWithFormat:@"¥%@", deal.price];
    
    UILabel *buyCountLabel = (UILabel *)[cell viewWithTag:40];
    buyCountLabel.text = [NSString stringWithFormat:@"%@人已购买", deal.buyCount];
    
    return cell;
}

@end
//
//  XMGDealsViewController.h
//  06-自定义等高cell01-storyboard
#import <UIKit/UIKit.h>

@interface XMGDealsViewController : UITableViewController

@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

06-自定义等高cell02-storyboard

//
//  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];
    
    
}

- (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 {
    static NSString *ID = @"deal";
    XMGDealCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 取出模型数据
    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;
@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

- (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
原文地址:https://www.cnblogs.com/laugh/p/6438508.html