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
相关文章推荐
- 用swagger-ui来生成webapi接口文档并可以在线测试
- Uiautomator API简介-摘要
- ios推送:本地通知UILocalNotification
- 1月12号 UIView
- hdoj5400Arithmetic Sequence【等差数列】
- IOS上 关于状态栏的相关设置(UIStatusBar)
- 运用Acronis True Image恢复系统
- android queryIntentActivities如何寻找出某个intent是否可用
- 解决UITableView设置了偏移量但是不偏移的问题
- Android studio 项目没有错误,运行按钮为灰色无法运行,rebuild 和 clean 都没有用
- NSOperationQueue
- 转 史上最完整交互设计基本原则
- easyui validatebox 验证类型
- 【代码笔记】点击任何处,显示出红色的UIView
- 【代码笔记】点击任何处,显示出红色的UIView
- RequireJS模块化加载
- 使用Fragment创建动态UI
- Implement Queue using Stacks
- 使用request简单爬虫
- UITabBarControllerDemo 一个最基本到标签控制器