天天看點

0917 純代碼、SB、XIB自定義Cell

0917 純代碼、SB、XIB自定義Cell

tableView dequeueReusableCellWith 如果用到SB中的Cell是兩個參數的。 沒有用到Storyboard中的Cell是一個參數。加上if(!cell)判斷。

需求右邊顯示圖檔,左邊顯示文本。

純代碼建立,一個參數。 1、Cell删掉。

0917 純代碼、SB、XIB自定義Cell

2、自己建立Cell類 繼承UITableViewCell 3、

0917 純代碼、SB、XIB自定義Cell

借用原本的初始化方法,重寫它

0917 純代碼、SB、XIB自定義Cell

自定義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。

0917 純代碼、SB、XIB自定義Cell

記得在TableViewController中設定行高,顯示效果才好。 -( CGFloat )tableView:( UITableView  *)tableView heightForRowAtIndexPath:( NSIndexPath  *)indexPath{

     return  200 ; } iv在.h中聲明成屬性。 不能起名字叫imageView. 原Cell控件和自己控件對比。

0917 純代碼、SB、XIB自定義Cell

xib和SB選擇; xib可以複用。SB不可。

0917 純代碼、SB、XIB自定義Cell

第二種:SB

辨別“Cell” Cell所屬的類修改為:

0917 純代碼、SB、XIB自定義Cell

若要修改Cell中的内容,其中都是你自定義 的控件。将Cell的屬性設定如下:

0917 純代碼、SB、XIB自定義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

0917 純代碼、SB、XIB自定義Cell
0917 純代碼、SB、XIB自定義Cell

控制 行

0917 純代碼、SB、XIB自定義Cell

- ( 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 重用[email protected]“Cell” 3、變成XIB的自定義Cell的ID。 4、在 cellForRowAtIndexPath方法中,判斷和生成cell的類都改為XIB自定義Cell類的。 

MyTableViewCell XIB 重用辨別:

0917 純代碼、SB、XIB自定義Cell

差別: SB中拖動Cell多高,模拟器顯示就是多高。 XIB拖動無法影響模拟器。 用方法 HeightForRow。

4作業:成績單一個Cell顯示四個文本。

0917 純代碼、SB、XIB自定義Cell
0917 純代碼、SB、XIB自定義Cell

預設展示  最左邊的。拖動item的順序就可以 改變。

0917 純代碼、SB、XIB自定義Cell

tableView dequeueReusableCellWith 如果用到SB中的Cell是兩個參數的。 沒有用到Storyboard中的Cell是一個參數。加上if(!cell)判斷。

需求右邊顯示圖檔,左邊顯示文本。

純代碼建立,一個參數。 1、Cell删掉。

0917 純代碼、SB、XIB自定義Cell

2、自己建立Cell類 繼承UITableViewCell 3、

0917 純代碼、SB、XIB自定義Cell

借用原本的初始化方法,重寫它

0917 純代碼、SB、XIB自定義Cell

自定義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。

0917 純代碼、SB、XIB自定義Cell

記得在TableViewController中設定行高,顯示效果才好。 -( CGFloat )tableView:( UITableView  *)tableView heightForRowAtIndexPath:( NSIndexPath  *)indexPath{

     return  200 ; } iv在.h中聲明成屬性。 不能起名字叫imageView. 原Cell控件和自己控件對比。

0917 純代碼、SB、XIB自定義Cell

xib和SB選擇; xib可以複用。SB不可。

0917 純代碼、SB、XIB自定義Cell

第二種:SB

辨別“Cell” Cell所屬的類修改為:

0917 純代碼、SB、XIB自定義Cell

若要修改Cell中的内容,其中都是你自定義 的控件。将Cell的屬性設定如下:

0917 純代碼、SB、XIB自定義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

0917 純代碼、SB、XIB自定義Cell
0917 純代碼、SB、XIB自定義Cell

控制 行

0917 純代碼、SB、XIB自定義Cell

- ( 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 重用[email protected]“Cell” 3、變成XIB的自定義Cell的ID。 4、在 cellForRowAtIndexPath方法中,判斷和生成cell的類都改為XIB自定義Cell類的。 

MyTableViewCell XIB 重用辨別:

0917 純代碼、SB、XIB自定義Cell

差別: SB中拖動Cell多高,模拟器顯示就是多高。 XIB拖動無法影響模拟器。 用方法 HeightForRow。

4作業:成績單一個Cell顯示四個文本。

0917 純代碼、SB、XIB自定義Cell
0917 純代碼、SB、XIB自定義Cell

預設展示  最左邊的。拖動item的順序就可以 改變。