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的顺序就可以 改变。