当前位置:网站首页>cell的定义
cell的定义
2022-07-25 09:21:00 【山河丘壑】
前言
在学习UITableView时遇到了cell。学习后进行了对自定义cell的总结
什么时候用需要自定义cell
当系统的单元格不满足我们的需求时,我们可以自定义一个继承UITableViewCell的类来实现自定义cell
自定义一个cell类后,如果需要有不同排版样式,需要重新自定义新的cell类
怎样自定义
我们之前的cell是通过initWithStyle来创建的
现在需要重写这个方法
步骤
- 新建一个myTableViewCell继承UITableViewCell
- 重写initWithStyle方法
- 在ViewController.m中包含我们的file.h
- 在ViewController.h中遵守协议UITableViewDelegate,UITableViewDataSource
- 实现
在本例中,需要向project中添加两个名为1.jpg和2.jpg的图片
第一步不解释
这里是对initWithStyle方法的重写。在myTableViewcell.m文件中
#import "myTableViewCell.h"
@implementation myTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style
reuseIdentifier:reuseIdentifier];
if ([self.reuseIdentifier isEqualToString:@"first"]) {
_label = [[UILabel alloc] init];
_label.text = @"nuonuo";
[self.contentView addSubview:_label];
_imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];
[self.contentView addSubview: _imageView];
_label.frame = CGRectMake(20, 10, 100, 100);
_imageView.frame = CGRectMake(100, 30, 100, 100);
}
if([self.reuseIdentifier isEqualToString:@"second"]){
_label = [[UILabel alloc] init];
_label.text = @"nana";
[self.contentView addSubview:_label];
_imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2.jpg"]];
[self.contentView addSubview: _imageView];
_label.frame = CGRectMake(20, 10, 100, 100);
_imageView.frame = CGRectMake(100, 30, 100, 100);
}
return self;
}
@end
第四步
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
<
UITableViewDelegate,
UITableViewDataSource
>
@property(nonatomic, strong) UITableView *tableView;
@end
第五步
#import "ViewController.h"
#import"myTableViewCell.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:_tableView];
[_tableView registerClass:[myTableViewCell class] forCellReuseIdentifier:@"first"];
[_tableView registerClass:[myTableViewCell class] forCellReuseIdentifier:@"second"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 160;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 2 == 0) {
myTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"first" forIndexPath:indexPath];
return cell;
} else {
myTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"second" forIndexPath:indexPath];
return cell;
}
}
@end
结果
这里测试时使用的是两组,展现出来的就是两组,每组两张图片两个标签
总结
自定义cell是为了简化程序步骤,但是当需要多种排版时,就需要定义多个cell了。
UITableView有两个,UITableViewStylePlain和UITableViewStyleGroup
边栏推荐
猜你喜欢
随机推荐
C#语言和SQL Server数据库技术
Redis set 结构命令
【代码源】每日一题 - 排队
OverTheWire-Bandit
[GYCTF2020]Node Game
作业7.21 约瑟夫环问题与进制转换
【cf】Round 128 C. Binary String
The interviewer asked: how to prevent oversold? There are several ways to achieve it
【代码源】 每日一题 素数之欢(bfs)
[GYCTF2020]Ez_Express
Mongodb exploration phase [easy to understand]
@3-1 CCF 2020-09-1 称检测点查询
一文搞懂为什么要同时重写equals方法和hashCode方法+实例分析
作业7.19 顺序表
How to deploy the jar package to the server? Note: whether the startup command has nohup or not has a lot to do with it
Data preprocessing
在Ubuntu中安装MySQL并创建新用户
数据预处理
【代码源】每日一题 算的我头都大啦
~1 ccf 2022-06-2 寻宝!大冒险!

![[HCTF 2018]admin](/img/d7/f0155c72d3fbddf0a8c1796a179a0f.png)





![[GYCTF2020]Ez_ Express](/img/ce/02b90708f215715bb53cacfd4c21f0.png)

