详细例子构建自定义cell

在实际做项目中,使用系统自带的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;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2014-06-30 00:09  在bug中前行  阅读(297)  评论(0编辑  收藏  举报