0917 纯代码、SB、XIB自定义Cell



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 ;
}

- ( instancetype )initWithStyle:( UITableViewCellStyle )style reuseIdentifier:( NSString  *)reuseIdentifier
{
   
  self  = [ super  initWithStyle :style  reuseIdentifier :reuseIdentifier];
   
  if  ( self ) {
        <#statements#>
    }
   
  return  self ;
}

添加一个图片和Lable。


记得在TableViewController中设置行高,显示效果才好。
-( CGFloat )tableView:( UITableView  *)tableView heightForRowAtIndexPath:( NSIndexPath  *)indexPath{
   
  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;
}

第三种:
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;
}


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 ;
}

- ( instancetype )initWithStyle:( UITableViewCellStyle )style reuseIdentifier:( NSString  *)reuseIdentifier
{
   
  self  = [ super  initWithStyle :style  reuseIdentifier :reuseIdentifier];
   
  if  ( self ) {
        <#statements#>
    }
   
  return  self ;
}

添加一个图片和Lable。


记得在TableViewController中设置行高,显示效果才好。
-( CGFloat )tableView:( UITableView  *)tableView heightForRowAtIndexPath:( NSIndexPath  *)indexPath{
   
  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;
}

第三种:
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;
}


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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值