詳細例子構建自訂cell

來源:互聯網
上載者:User

標籤:style   color   使用   strong   width   檔案   

在實際做項目中,使用系統內建的tableView時,cell的樣式單一,不易改變。而使用xib時,能改變cell的樣式,但是項目不具有可改性,xib 一旦創立,內容不會改變,這裡,利用封裝的思想,用純程式碼建立自訂cell,資料使用plist檔案儲存體

1、首先建立一個資料模型(moreBang),在標頭檔中(moreBang.h)定義模型中的屬性,並將字典轉為模型資料:

     在moreBang.m中實現方法:(在對象方法中還可以使用KVC轉化)

2、其次,在建立cell的frame模型,設定各個空間的位置,建立moreBangFrame.h,並將資料模型傳入

    在moreBangFrame.m檔案中設定frame:

3、建立moreBangCell,繼承UITableViewCell,moreBangCell.h的設定如下

    #import <UIKit/UIKit.h>

   @class moreBang,moreBangFrame;

   @interface moreBangCell : UITableViewCell

   @property(nonatomic,strong) moreBangFrame * morebangframe;

   // 重寫cell方法

   +(instancetype)cellWithTableView:(UITableView *)tableView;

   @end

    在moreBangCell.m檔案中對模型資料進行設定

    #import "moreBangCell.h"

    #import "moreBang.h"

    #import "moreBangFrame.h"

    #import "UIImage+HL.h"

    @interface moreBangCell()

    @property(nonatomic,weak) UIImageView * iconView;

    @property(nonatomic,weak) UILabel * nameView;

    @property(nonatomic,weak) UILabel * textView;

    @end

    @implementation moreBangCell

    +(instancetype)cellWithTableView:(UITableView *)tableView

  {

        // 1、建立cell

        static NSString *ID = @"Cell";

        moreBangCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

        if (cell ==nil) {

            cell = [[moreBangCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];

            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        }

        return cell;

}

     // 在這個方法中添加cell的子控制項

      -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

    {

       self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];  

       self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

       if (self) {

        // 頭像

        UIImageView * iconView = [[UIImageView alloc]init];

        [self.contentView addSubview:iconView];

        self.iconView = iconView;

        // 暱稱

        UILabel * nameView = [[UILabel alloc]init];

        nameView.font = [UIFont systemFontOfSize:12];

        [self.contentView addSubview:nameView];

        self.nameView = nameView;        

        // 本文

        UILabel * textView = [[UILabel alloc]init];

        textView.numberOfLines = 0;

        textView.font = [UIFont systemFontOfSize:12];

        [self.contentView addSubview:textView];

        self.textView = textView;

    }

    return self;    

}

 

-(void)setMorebangframe:(moreBangFrame *)morebangframe

{

    _morebangframe = morebangframe;    

    //設定子控制項的frame

    [self settingFrame];   

    //給子控制項賦值資料

     [self settingData];    

/**

 *  設定資料

 */ 

-(void)settingData

{

    // 映像資料

    self.iconView.image = [UIImage imageWithNamed:_morebangframe.morebang.icon];

    // 暱稱

    self.nameView.text = _morebangframe.morebang.name;

    // 本文

    self.textView.text = _morebangframe.morebang.text;

}

 

-(void)settingFrame

{

        // 設定映像

    self.iconView.frame = _morebangframe.iconF;

    self.nameView.frame =  _morebangframe.nameF;

       // 設定本文

     self.textView.frame = _morebangframe.textF;

}

 @end

4、在控制器中匯入所需的標頭檔,定義一個數組:

   @property(nonatomic,strong) NSArray * myMorebangs;

  載入資料庫,懶載入:

    將轉換好的資料對應給每行cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

 moreBangCell *cell = [moreBangCell cellWithTableView:tableView];

 cell.morebangframe = self.myMorebangs[indexPath.row];

       // 返回cell

    return cell;

}

      

#pragma mark -設定每組的高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    // 擷取當前索引的frame

    moreBangFrame * moreBangFrame = self.myMorebangs[indexPath.row];

    // 返回美航的行高

    return moreBangFrame.cellHeight;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.