[iOS开发]-自定义cell

过程

  1. 新建UITableViewCell类型的.h和.m文件
  2. 在UITableViewCell类型的.h文件中添加需要使用的属性
  3. 在UITableViewCell类型的.m文件中写固定的两个方法
  4. 在ViewController.m文件中进行应用

一、新建 UITableViewCell类型的文件

过程如图:
在这里插入图片描述

二、在.h文件中添加需要使用的属性

下面以一个label一个imageView和一个button为例:

@interface testTableViewCell : UITableViewCell

@property (nonatomic, strong) UILabel *label;
 
@property (nonatomic, strong) UIButton *button;

@property (nonatomic, strong) UIImageView *imageView;

@end

三、在.m文件中写固定的方法

@implementation testTableViewCell

//固定方法一:
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    self.label = [[UILabel alloc] init];
    [self.contentView addSubview:_label];
    
	self.button = [[UIButton alloc] init];
    [self.contentView addSubview:self.button];
	
	self.imageView = [[UIImageView alloc] init] ;
    [self.contentView addSubview: _imageView];

	return self;
}

//固定方法二:
- (void)layoutSubviews {
    //设置button的位置和图片等
    self.Button.frame = CGRectMake(15, 15, 60, 60);
    [self.Button setImage:[UIImage imageNamed:@"xxx.png"] forState:UIControlStateNormal];

	//设置label的位置及文字
    _label.frame = CGRectMake(85, 32, 100, 30);
    _label.text = @"XXX";

	//设置imageView的位置和图片
	_imageView.frame = CGRectMake(150, 150, 100, 100);
	_imageView.image = [UIImage imageNamed: @"xxx.jpg"];
}

cell的自定义就完成了

四、在ViewController.m文件中应用

首先需要在ViewController.h文件中加入协议

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
<
UITableViewDelegate,
UITableViewDataSource
>
//定义一个数据视图对象
@property (nonatomic, strong)UITableView* tableView;

@end;

然后在ViewController.m中应用:

#import "ViewController.h"
#import "testTableViewCell.h"

@implementation ViewController

- (void)viewDidLoad {
	[super viewDidLoad];
	
	//创建数据视图
	_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

	//设置代理
    tableView.delegate = self;
    tableView.dataSource = self;
	
	//对cell进行注册
    [tableView registerClass:[TAYTableViewCell class] forCellReuseIdentifier:@"cellFirst"];
    
    //将数据视图添加到主视图上
    [self.view addSubview:_tableView];
}

//设置数据视图的组数
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 6;
}

//获取每组单元格的个数
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return 1;
}	
	
//获取单元格高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60
}

//创建单元格对象函数(创建几个单元格下面的函数就要被调用几次)
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {	
    
	testTableViewCell *cellFirst = [tableView dequeueReusableCellWithIdentifier:@"cellFirst" forIndexPath:indexPath]
	
	//设置自定义cell中按钮的信息
	[cellFirst.button addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];

	return cellFirst;
}
//其中注册过的cell不需要判空查看是否可以复用
//注册是为某一identifier 注册一个Class
//当标识符为identifier 的Cell队列中没有可复用的cell时,系统会自动创建一个绑定的Class类型的cell,所以无需自己判空

//按钮的事件函数
- (void) pressButton {
	
}

@end

以上就是自定义cell的过程

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值