iOS自定义UITableView的cell

本文介绍了一个自定义UITableViewCell的实现过程,包括如何加载XIB文件、设置属性和展示数据模型等关键步骤。此外还提供了一个数据模型类用于传递数据。
Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

//-------该类用来管理MLTgCell.xib
#import <UIKit/UIKit.h>
@class MLTg;
@interface MLTgCell : UITableViewCell

//将团购模型传给cell
@property(nonatomic, strong) MLTg *tg;

-(void) setTg:(MLTg *)tg;

+(instancetype)cellWithTableView:(UITableView *)tableView;
-(instancetype)initWithTableView:(UITableView *)tableView;

@end

#import "MLTgCell.h"
#import "MLTg.h"
@interface MLTgCell()
@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 MLTgCell

-(instancetype)initWithTableView:(UITableView *)tableView{
    static NSString *ID = @"tg";
    MLTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        //使用xib来创建自定义cell
        //---在xib中需要给cell设置标识tg,否则缓存池中取不到带有ID标识的cell.
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MLTgCell" owner:nil options:nil] firstObject];
    }
    return cell;
}


+(instancetype)cellWithTableView:(UITableView *)tableView{
    return [[self alloc] initWithTableView:tableView];
}
-(void) setTg:(MLTg *)tg{
    _tg = tg;
    //设置图片
    self.iconView.image = [UIImage imageNamed:tg.icon];
    //设置title
    self.titleView.text = tg.title;
    //设置价格
    self.priceView.text = [NSString stringWithFormat:@"¥%@", tg.price];
    //设置购买的数量
    self.buyCountView.text = [NSString stringWithFormat:@"%@人已购买",tg.buyCount];
}

@end

//----------数据模型类,用来传递数据-------
#import <Foundation/Foundation.h>

@interface MLTg : NSObject

@property(nonatomic, copy) NSString *buyCount;
@property(nonatomic, copy) NSString *icon;
@property(nonatomic, copy) NSString *price;
@property(nonatomic, copy) NSString *title;

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

@end

#import "MLTg.h"

@implementation MLTg

+(instancetype)tgWithDict:(NSDictionary *)dict{
    return [[self alloc]initWithDict:dict];
}
-(instancetype)initWithDict:(NSDictionary *)dict{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

@end


//-------控制器中用来返回自定义cell的方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //创建cell(屏蔽了cell是通过什么方式创建的(代码或xib方式))
    MLTgCell *cell = [MLTgCell cellWithTableView:tableView];
    
    //给cell传递模型数据.
    cell.tg = self.tgs[indexPath.row];

    return cell;
}


转载于:https://my.oschina.net/u/2285956/blog/354696

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值