iOS之UITableView Cell重用机制

/**

 *  设置TableViewCell每一行数据

 */

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

{

    

    WActiveCellVo *activeVo = self.activeCellVoMutableArray[indexPath.row];


      //1.创建Cell

     WActiveCell *cell = [WActiveCell cellWithTableView:tableView];

                

        cell.activeCellVo = activeVo;

        

        return cell;


}


自定义Cell

#import <UIKit/UIKit.h>

#import "WActiveCellVo.h"


@interface WActiveCell : UITableViewCell


/**

 *  通过Tableview创建一个Cell

 */

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


/**

 *  活动首页数据模型

 */

@property (strong, nonatomic) WActiveCellVo *activeCellVo;


@end



#import "WActiveCell.h"

#import "UIImageView+WebCache.h"


@interface WActiveCell()


/**

 *  活动背景图:异步加载

 */

@property (weak, nonatomic) UIImageView *activeIconView;


@property (weak, nonatomic) UILabel             *activeTitleLbl;

@property (weak, nonatomic) UILabel             *activeAddressLbl1;

@property (weak, nonatomic) UILabel             *activeAddressLbl2;

@property (weak, nonatomic) UIImageView         *activeTimeIconView;

@property (weak, nonatomic) UILabel             *activeTimeLbl;

@property (weak, nonatomic) UIImageView         *activeAddressIconView;


@end


@implementation WActiveCell


+(instancetype)cellWithTableView:(UITableView *)tableView

{

    static NSString *ID = @"WActiveCellIdentify";

    //从缓存池中取出

    WActiveCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    

    if (cell == nil) {

        //xib中加载cell

        //cell = [[[NSBundle mainBundle] loadNibNamed:@"WActiveCell" owner:nil options:nil] lastObject];

        cell = [[ WActiveCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

        cell.selectionStyle = UITableViewCellSelectionStyleBlue;

    }

    

    return cell;

}


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

{

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

    

    if (self) {

        //0.活动背景图片

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

        self.activeIconView = activeIconView;

        [self.contentView addSubview:activeIconView];

        

        //1.活动标题

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

        activeTitleLbl.textColor = [UIColor whiteColor];

        activeTitleLbl.font = [UIFont boldSystemFontOfSize:16];

        self.activeTitleLbl = activeTitleLbl;

        [self.contentView addSubview:activeTitleLbl];

        

        //2.活动地点1

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

        activeAddressLbl1.textColor = [UIColor whiteColor];

        activeAddressLbl1.font = [UIFont systemFontOfSize:14];

        self.activeAddressLbl1 = activeAddressLbl1;

        [self.contentView addSubview:activeAddressLbl1];

        

        //3.1活动时间图标

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

        activeTimeIconView.image = [UIImage imageNamed:@"activities_clock"];

        activeTimeIconView.contentMode = UIViewContentModeCenter;

        self.activeTimeIconView = activeTimeIconView;

        [self.contentView addSubview:activeTimeIconView];

        

        //3.2活动时间

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

        activeTimeLbl.textColor = [ UIColor blueColor];

        activeTimeLbl.font = [UIFont systemFontOfSize:12];

        self.activeTimeLbl = activeTimeLbl;

        [self.contentView addSubview:activeTimeLbl];

        

        //4.1活动地点图标

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

        activeAddressIconView.image = [UIImage imageNamed:@"activities_location"];

        activeAddressIconView.contentMode = UIViewContentModeCenter;

        self.activeAddressIconView = activeAddressIconView;

        [self.contentView addSubview:activeAddressIconView];

        

        //4.2活动地点2

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

        activeAddressLbl2.textColor = [UIColor blueColor];

        activeAddressLbl2.font = [UIFont systemFontOfSize:12];

        self.activeAddressLbl2 = activeAddressLbl2;

        [self.contentView addSubview:activeAddressLbl2];

    }

    

    return self;

    

}


-(void)setActiveCellVo:(WActiveCellVo *)activeCellVo

{

    _activeCellVo = activeCellVo;

    

    //1.设置数据

    [self settingData];

    

    //2.设置frame

    [self settingFrame];

    

}

/**

 *  根据数据模型设置Cell子控件显示

 */

-(void)settingData

{

}


/**

 *  根据模型设置Cell内部子控件的大小

 */

-(void)settingFrame

{

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值