cell自定义方式一 高度一致时



//——----------------- 当cell的高度一致时使用XIB --------------------


步骤:

1.> 新建一个 xib 文件描述一个 view 的内部结构 ( 假设叫做 MJTgCell.xib)  
 
2.> 新建一个自定义的类
  ( 自定义类需要继承自系统自带的 view, 继承自哪个类 ,   取决于 xib 根对象的 Class)
 
3.> 新建类的类名最好跟 xib 的文件名保持一致 ( 比如类名就叫做 MJTgCell)  
 
4.> xib 中的控件     自定义类的 .m 文件   进行连线  
 
5.> 提供一个类方法返回一个创建好的自定义 view( 屏蔽从 xib 加载的过程 )  
 
6.> 提供一个模型属性让外界传递模型数据  
 
7.> 重写模型属性的 setter 方法 , 在这里将模型数据展示到对应的子控件上面


//——---------------------———— ———————模型文件M ----------------------------------------
继承于NSObject

.h
#import <Foundation/Foundation.h>

@interface MJTg : NSObject
/**
 *  标题
 */
@property (nonatomic,copy)NSString *title;
/**
 *  价格
 */
@property (nonatomic,copy)NSString *price;
/**
 *  图片
 */
@property (nonatomic,copy)NSString *icon;
/**
 *  购买人数
 */
@property (nonatomic,copy)NSString *buyCount;

+ (instancetype)tgWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end


.m
#import "MJTg.h"

@implementation MJTg

+ (instancetype)tgWithDict:(NSDictionary *)dict
{
    return [[selfalloc]initWithDict:dict];
}

- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [superinit]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}
@end



//——---------------------———— ———————视图文件V ----------------------------------------

继承于UITableViewCell

.h
#import <UIKit/UIKit.h>

@class MJTg;

@interface MJTgCell : UITableViewCell

/**
 *  通过一个tableView来创建一个cell
 */
+ (instancetype)cellWithTableView:(UITableView *)tableView;

/**
 *  团购模型
 */
@property (nonatomic,strong)MJTg *tg;
@end

 


XIB文件,通过脱线获得属性,红圈处可以设置cell的ID标示



m
#import "MJTgCell.h"
#import "MJTg.h"

@interface MJTgCell()

//这些属性均从xib中做了连线
@property (weak,nonatomic)IBOutlet UIImageView *iconView;
@property (weak,nonatomic)IBOutlet UILabel *titleView;
@property (weak,nonatomic)IBOutlet UILabel *priceView;
@property (weak,nonatomic)IBOutlet UILabel *buyCountView;

@end

@implementation MJTgCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"tg";
    MJTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        // 从xib中加载cell
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MJTgCell"owner:niloptions:nil]lastObject];
    }
    return cell;
}

- (void)setTg:(MJTg *)tg
{
    _tg = tg;
   
    // 1.图片
    self.iconView.image = [UIImageimageNamed:tg.icon];
   
    // 2.标题
    self.titleView.text = tg.title;
   
    // 3.价格
    self.priceView.text = [NSStringstringWithFormat:@"¥%@", tg.price];
   
    // 4.购买数
    self.buyCountView.text = [NSStringstringWithFormat:@"%@人已购买", tg.buyCount];
}

@end



//——---------------------———— ———视图控制器文件C ----------------------------------------

继承于UIViewController

@property (nonatomic,strong)NSMutableArray *tgs;

/**
 *  数据的懒加载
 */
- (NSMutableArray *)tgs
{
    if (_tgs ==nil) {
        // 初始化
        // 1.获得plist的全路径
        NSString *path = [[NSBundlemainBundle]pathForResource:@"tgs.plist"ofType:nil];
       
        // 2.加载数组
        NSArray *dictArray = [NSArrayarrayWithContentsOfFile:path];
       
        // 3.将dictArray里面的所有字典转成模型对象,放到新的数组中
        NSMutableArray *tgArray = [NSMutableArrayarray];
        for (NSDictionary *dictin dictArray) {
            // 3.1.创建模型对象
            MJTg *tg = [MJTgtgWithDict:dict];
           
            // 3.2.添加模型对象到数组中
            [tgArray addObject:tg];
        }
       
        // 4.赋值
        _tgs = tgArray;
    }
    return _tgs;
}

#pragma mark - 数据源方法
/**
 *  一共有多少行数据
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tgs.count;
}

/**
 *  每一行显示怎样的cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.创建cell
    MJTgCell *cell = [MJTgCellcellWithTableView:tableView];
   
    // 2.给cell传递模型数据
    cell.tg = self.tgs[indexPath.row];
    return cell;
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值