iOS代码自定义UITableView Cell(每个Cell的高度不一样)
2015-04-21 23:35
525 查看
1.新建一个继承自UITableViewCell的类 @interface ViewController : UITableViewController
2.重写initWithStyle:reuseIdentifier:方法
添加所有需要显示的子控件(不需要设置子控件的数据和frame, 子控件要添加到contentView中)
进行子控件一次性的属性设置(有些属性只需要设置一次, 比如字体\固定的图片)
@interface StatusCell()
/**
* 头像
*/
@property (nonatomic, weak) UIImageView *iconView;
/**
* 昵称
*/
@property (nonatomic, weak) UILabel *nameView;
@end
@implementation StatusCell
/**
* 构造方法(在初始化对象的时候会调用)
* 一般在这个方法中添加需要显示的子控件
*/
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// 1.头像
UIImageView *iconView = [[UIImageView alloc] init];
[self.contentView addSubview:iconView];
self.iconView = iconView;
// 2.昵称
UILabel *nameView = [[UILabel alloc] init];
nameView.font = NameFont;
[self.contentView addSubview:nameView];
self.nameView = nameView;
}
return self;
}
3.提供2个模型
data 数据模型: 存放文字数据\图片数据
@interface Status : NSObject
@property (nonatomic, copy) NSString *name; // 内容
@property (nonatomic, copy) NSString *icon; // 头像
@end
frame模型: 存放数据模型\所有子控件的frame\cell的高度,最好设置readonly属性,防止被意外修改
/**
* 昵称的frame
*/
@property (nonatomic, assign, readonly) CGRect nameF;
4.Cell设置加载中拥有一个frame模型(不要直接拥有数据模型)
5.重写frame模型属性的setter方法: 在这个方法中设置子控件的显示数据和frame
/**
* 在这个方法中设置子控件的frame和显示数据
*/
- (void)setStatusFrame:(StatusFrame *)statusFrame
{
_statusFrame = statusFrame;
// 1.设置数据
[self settingData];
// 2.设置frame
[self settingFrame];
}
/**
* 设置数据
*/
- (void)settingData
{
// 微博数据
Status *status = self.statusFrame.status;
// 1.头像
self.iconView.image = [UIImage imageNamed:status.icon];
// 2.昵称
self.nameView.text = status.name;
}
/**
* 计算文字尺寸
*
* @param text 需要计算尺寸的文字
* @param font 文字的字体
* @param maxSize 文字的最大尺寸
*/
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
NSDictionary *attrs = @{NSFontAttributeName : font};
return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
6.frame模型数据的初始化已经采取懒加载的方式(每一个cell对应的frame模型数据只加载一次)
/**
* 在这个方法中设置子控件的frame和显示数据
*/
- (void)setStatusFrame:(StatusFrame *)statusFrame
{
_statusFrame = statusFrame;
// 1.设置数据
[self settingData];
// 2.设置frame
[self settingFrame];
}
demo下载地址: http://download.csdn.net/detail/linwenbang/8618345
2.重写initWithStyle:reuseIdentifier:方法
添加所有需要显示的子控件(不需要设置子控件的数据和frame, 子控件要添加到contentView中)
进行子控件一次性的属性设置(有些属性只需要设置一次, 比如字体\固定的图片)
@interface StatusCell()
/**
* 头像
*/
@property (nonatomic, weak) UIImageView *iconView;
/**
* 昵称
*/
@property (nonatomic, weak) UILabel *nameView;
@end
@implementation StatusCell
/**
* 构造方法(在初始化对象的时候会调用)
* 一般在这个方法中添加需要显示的子控件
*/
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// 1.头像
UIImageView *iconView = [[UIImageView alloc] init];
[self.contentView addSubview:iconView];
self.iconView = iconView;
// 2.昵称
UILabel *nameView = [[UILabel alloc] init];
nameView.font = NameFont;
[self.contentView addSubview:nameView];
self.nameView = nameView;
}
return self;
}
3.提供2个模型
data 数据模型: 存放文字数据\图片数据
@interface Status : NSObject
@property (nonatomic, copy) NSString *name; // 内容
@property (nonatomic, copy) NSString *icon; // 头像
@end
frame模型: 存放数据模型\所有子控件的frame\cell的高度,最好设置readonly属性,防止被意外修改
/**
* 昵称的frame
*/
@property (nonatomic, assign, readonly) CGRect nameF;
4.Cell设置加载中拥有一个frame模型(不要直接拥有数据模型)
5.重写frame模型属性的setter方法: 在这个方法中设置子控件的显示数据和frame
/**
* 在这个方法中设置子控件的frame和显示数据
*/
- (void)setStatusFrame:(StatusFrame *)statusFrame
{
_statusFrame = statusFrame;
// 1.设置数据
[self settingData];
// 2.设置frame
[self settingFrame];
}
/**
* 设置数据
*/
- (void)settingData
{
// 微博数据
Status *status = self.statusFrame.status;
// 1.头像
self.iconView.image = [UIImage imageNamed:status.icon];
// 2.昵称
self.nameView.text = status.name;
}
/**
* 计算文字尺寸
*
* @param text 需要计算尺寸的文字
* @param font 文字的字体
* @param maxSize 文字的最大尺寸
*/
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
NSDictionary *attrs = @{NSFontAttributeName : font};
return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
6.frame模型数据的初始化已经采取懒加载的方式(每一个cell对应的frame模型数据只加载一次)
/**
* 在这个方法中设置子控件的frame和显示数据
*/
- (void)setStatusFrame:(StatusFrame *)statusFrame
{
_statusFrame = statusFrame;
// 1.设置数据
[self settingData];
// 2.设置frame
[self settingFrame];
}
demo下载地址: http://download.csdn.net/detail/linwenbang/8618345
相关文章推荐
- ios-day07-02(模仿新浪微博:UITableView中cell的高度不一致,使用代码来自定义cell、UITableViewController的使用)
- 一. 使用代码实现UITableView的cell自定义高度-类似于微博主页
- IOS-UITableView 及自定义cell
- IOS 自定义每行高度不同的UITableView
- ios TableView那些事(三十 五)TableView 单选操作使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- UITableView中自定义Cell,自适应高度
- iOS开发中实现UITableView的Cell左划删除等自定义功能
- iOS中UITableView Cell实现自定义单选功能
- 【iOS7开发笔记】tableview之通过代码自定义cell(cell的高度不一致)
- 【iOS】UITableView Cell自定义单选功能
- iOS开发-UI控件:动态改变UITableView中的Cell高度
- 【iOS】Masonry和FDTemplateLayoutCell搭配使用「UITableview自适应内容高度」
- iOS UITableView cell自适应内容高度
- IOS UItableview UIcollectionview cell高度自适应
- [iOS]技巧集锦:UITableView自定义Cell中的控件无法完全对齐Cell的左边界和右边界
- UI基础之UITableView案例微博----自定义cell利用代码
- AutoLayout自定义tableViewCell --- Masonry + UITableView+FDTemplateLayoutCell 纯代码实现
- [IOS]UITableView重用(使用多个自定义Cell)
- iOS UITableView Cell的动态高度实现
- iOS:在UITableView里使用自定义Cell的那点事(简要总结)