0917 纯代码、SB、XIB自定义Cell
来源:互联网 发布:office办公软件免费版 编辑:程序博客网 时间:2024/05/22 02:08
tableView dequeueReusableCellWith
如果用到SB中的Cell是两个参数的。
没有用到Storyboard中的Cell是一个参数。加上if(!cell)判断。
需求右边显示图片,左边显示文本。
纯代码创建,一个参数。
1、Cell删掉。
2、自己创建Cell类
继承UITableViewCell
3、
借用原本的初始化方法,重写它
自定义Cell
初始化方法 重写。
写自己想添加的内容。
直接
init-with-frame敲出来 替换掉。
- (instancetype)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
<#statements#>
}
return self;
{
self = [super initWithFrame:frame];
if (self) {
<#statements#>
}
return self;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
<#statements#>
}
return self;
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
<#statements#>
}
return self;
}
添加一个图片和Lable。
记得在TableViewController中设置行高,显示效果才好。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 200;
return 200;
}
iv在.h中声明成属性。
不能起名字叫imageView.
原Cell控件和自己控件对比。
xib和SB选择;
xib可以复用。SB不可。
第二种:SB
标识“Cell”
Cell所属的类修改为:
若要修改Cell中的内容,其中都是你自定义
的控件。将Cell的属性设置如下:
关联到MyTableViewCell.h中。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 如果是纯代码创建 就是dequeue方法 一个参数 如果是sb创建 就是两个参数
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
// cell.iv.image = [UIImage imageNamed:@""];
cell.myLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
return cell;
// 如果是纯代码创建 就是dequeue方法 一个参数 如果是sb创建 就是两个参数
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
// cell.iv.image = [UIImage imageNamed:@""];
cell.myLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
return cell;
}
第三种:
XIB创建自定义Cell
ViewController中拖拽一个TableView
Delegate DataSource连线
两个方法实现。
创建自定义Cell类。勾选XIB
控制 行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"MyTableViewCell" owner:self options:nil]lastObject];
}
return cell;
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"MyTableViewCell" owner:self options:nil]lastObject];
}
return cell;
}
1、在原来的TableViewController 中的那个系统Cell
2、删除cellForRowAtIndexPath 重用ID@“Cell”
3、变成XIB的自定义Cell的ID。
4、在cellForRowAtIndexPath方法中,判断和生成cell的类都改为XIB自定义Cell类的。
MyTableViewCell XIB 重用标识:
区别:
SB中拖动Cell多高,模拟器显示就是多高。
XIB拖动无法影响模拟器。
用方法 HeightForRow。
4作业:成绩单一个Cell显示四个文本。
默认展示 最左边的。拖动item的顺序就可以 改变。
tableView dequeueReusableCellWith
如果用到SB中的Cell是两个参数的。
没有用到Storyboard中的Cell是一个参数。加上if(!cell)判断。
需求右边显示图片,左边显示文本。
纯代码创建,一个参数。
1、Cell删掉。
2、自己创建Cell类
继承UITableViewCell
3、
借用原本的初始化方法,重写它
自定义Cell
初始化方法 重写。
写自己想添加的内容。
直接
init-with-frame敲出来 替换掉。
- (instancetype)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
<#statements#>
}
return self;
{
self = [super initWithFrame:frame];
if (self) {
<#statements#>
}
return self;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
<#statements#>
}
return self;
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
<#statements#>
}
return self;
}
添加一个图片和Lable。
记得在TableViewController中设置行高,显示效果才好。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 200;
return 200;
}
iv在.h中声明成属性。
不能起名字叫imageView.
原Cell控件和自己控件对比。
xib和SB选择;
xib可以复用。SB不可。
第二种:SB
标识“Cell”
Cell所属的类修改为:
若要修改Cell中的内容,其中都是你自定义
的控件。将Cell的属性设置如下:
关联到MyTableViewCell.h中。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 如果是纯代码创建 就是dequeue方法 一个参数 如果是sb创建 就是两个参数
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
// cell.iv.image = [UIImage imageNamed:@""];
cell.myLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
return cell;
// 如果是纯代码创建 就是dequeue方法 一个参数 如果是sb创建 就是两个参数
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
// cell.iv.image = [UIImage imageNamed:@""];
cell.myLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
return cell;
}
第三种:
XIB创建自定义Cell
ViewController中拖拽一个TableView
Delegate DataSource连线
两个方法实现。
创建自定义Cell类。勾选XIB
控制 行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"MyTableViewCell" owner:self options:nil]lastObject];
}
return cell;
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"MyTableViewCell" owner:self options:nil]lastObject];
}
return cell;
}
1、在原来的TableViewController 中的那个系统Cell
2、删除cellForRowAtIndexPath 重用ID@“Cell”
3、变成XIB的自定义Cell的ID。
4、在cellForRowAtIndexPath方法中,判断和生成cell的类都改为XIB自定义Cell类的。
MyTableViewCell XIB 重用标识:
区别:
SB中拖动Cell多高,模拟器显示就是多高。
XIB拖动无法影响模拟器。
用方法 HeightForRow。
4作业:成绩单一个Cell显示四个文本。
默认展示 最左边的。拖动item的顺序就可以 改变。
0 0
- 0917 纯代码、SB、XIB自定义Cell
- 使用纯代码自定义cell
- 通过xib和代码自定义cell
- swift 纯代码、xib自定义控件
- 第七天,xib 定义tableviewcell以及纯代码定义cell
- xib自定义cell代码怎么加载 代码加载xib的cell
- 自定义等高的cell(纯代码)
- 纯代码自定义cell的过程
- 纯代码实现自定义UITableView的cell
- 使用xib自定义Cell
- 自定义cell 用xib
- xib 自定义cell
- 通过xib自定义cell
- iOS--xib自定义cell
- iOS ---xib自定义cell
- xib 自定义cell高度
- 通过xib自定义cell
- 纯代码自定义cell(cell高度不一致)
- spring集成rabbitmq
- 在windows下查看PCI或PCIE設備的vendor ID和device ID
- 循环和文本输入
- struts2.3.3配置以及所需的jar包
- 站内seo优化如何做?
- 0917 纯代码、SB、XIB自定义Cell
- 深入理解Java:注解(Annotation)基本概念
- Maven搭建SpringMVC+Mybatis项目详解
- SSL通信过程分析
- 空间域增强
- 日志崩溃重新启动
- MVP设计模式
- 概率图模型(PGM)学习笔记(二)贝叶斯网络-语义学与因子分解
- 0917 Cell分组 静态Cell xib创建页面 自定义控件