您的位置:首页 > 产品设计 > UI/UE

197,自定义UITableViewCell

2016-01-12 10:05 323 查看




思路:新建一个xib,设置界面,创建一个Cocoa Touch Class,继承UITableViewCell。然后,让xib跟类相关联,接着,设置该cell的标识符和相关的方法,属性,用于缓存取cell。

代码如下:

JSTgs.h:

#import <UIKit/UIKit.h>

@class JSTgs;

@interface JSTgCell :UITableViewCell

@property (nonatomic,strong)JSTgs *tg;

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

@end

JSTgs.m:

#import "JSTgCell.h"

#import "JSTgs.h"

@interface JSTgCell ()

@property (weak,
nonatomic) IBOutletUIImageView *icon;

@property (weak,
nonatomic) IBOutletUILabel *title;

@property (weak,
nonatomic) IBOutletUILabel *price;

@property (weak,
nonatomic) IBOutletUILabel *buyCount;

@end

@implementation JSTgCell

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

static NSString *ID =@"cell";

JSTgCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID];

if (cell == nil) {

cell = [[[NSBundlemainBundle]loadNibNamed:@"JSTgCell"owner:niloptions:nil]lastObject];

}

return cell;

}

-(void)setTg:(JSTgs *)tg{

_tg = tg;

self.icon.image = [UIImageimageNamed:tg.icon];

self.title.text = tg.title;

self.price.text = [NSStringstringWithFormat:@"¥%@",tg.price];

self.buyCount.text = [NSStringstringWithFormat:@"%@人购买",tg.buyCount];

}

#pragma mark - 模板提供的方法

///**

// 初始化方法

//

// 使用代码创建Cell的时候会被调用,如果使用XIB或者Storyboard,此方法不会被调用

// */

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

//{

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

// if (self) {

// NSLog(@"%s", __func__);

// }

// return self;

//}

//

///**

// 从XIB被加载之后,会自动被调用,如果使用纯代码,不会被执行

// */

//- (void)awakeFromNib

//{

// NSLog(@"%s", __func__);

// self.contentView.backgroundColor = [UIColor clearColor];

//}

//

///**

// Cell 被选中或者取消选中是都会被调用

//

// 如果是自定义Cell控件,所有的子控件都应该添加到contentView中

// */

//- (void)setSelected:(BOOL)selected animated:(BOOL)animated

//{

// [super setSelected:selected animated:animated];

//

// if (selected) {

// self.contentView.backgroundColor = [UIColor redColor];

// } else {

// self.contentView.backgroundColor = [UIColor greenColor];

// }

//}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: