UI 一一 自定义等高cell (storyboard方式)

本文介绍如何在iOS开发中使用Storyboard和XIB来实现自定义TableViewCell,并展示了具体的代码实现方式,包括如何加载自定义Cell、设置Cell属性以及如何在TableView中显示不同类型的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用storyboard 和 xib的方式基本类似,

长话短说直接上代码.


ZYTg文件

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

ZYTgCell文件


#import <UIKit/UIKit.h>  
  
@class ZYTg;  
@interface ZYTgCell : UITableViewCell  
  
/** ZYTg模型 */  
@property(nonatomic,strong)ZYTg * tg;  
  
@end  
  
#import "ZYTg.h"  
  
@interface ZYTgCell ()  
  
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;  
  
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;  
  
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;  
  
@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;  
  
@end  
  
@implementation ZYTgCell  
  
// 重写set方法,设置数据  
- (void)setTg:(ZYTg *)tg  
{  
    _tg = tg;  
    self.iconImageView.image = [UIImage imageNamed:tg.icon];  
    self.titleLabel.text = tg.title;  
    self.priceLabel.text = [NSString stringWithFormat:@"¥%@",tg.price];  
    self.buyCountLabel.text = [NSString stringWithFormat:@"%@人已购买",tg.buyCount];  
      
}  
  
@end 

Main.storyboard 文件



设置不同类型的cell,在storyboard中



ViewController 文件

#import "ViewController.h"
#import "MJExtension.h"
#import "ZYTgCell.h"
#import "ZYTg.h"

@interface ViewController ()

@property (nonatomic,strong) NSArray *tgs;

@end

@implementation ViewController

- (NSArray *)tgs
{
    if (_tgs == nil) {
        
        _tgs = [ZYTg mj_objectArrayWithFilename:@"tgs.plist"];
    }
    return _tgs;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.rowHeight = 70;
}

#pragma -mark 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tgs.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row % 2 == 0) {
        // 创建一个重用标识,去缓存池中取ID标识的cell
        static NSString *ID = @"tg";
        ZYTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        // 设置每一行cell的数据
        cell.tg = self.tgs[indexPath.row];
        return cell;
    }else{
        
        return [tableView dequeueReusableCellWithIdentifier:@"test"];
    }
    
}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

white camel

感谢支持~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值