//***************************************************************************** 04 自定义的 cell
//目的是来显示消息的
微博项目:
步骤:1.创建一个CZMicroBlogCell 继承UITableViewCell
2.导入头文件 #import "CZMicroBlogCell.h"//(在ViewController.m 中)
3.接着写数据源的方法 // 具体操作参考 04-实现数据源的方法
设置组//如果只有一组的话 就可以不用写这行代码 return 1;
设置行
设置cell (1)//创建可重用的自定义cell
CZMicroBlogCell *cell =[CZMicroBlogCell microBlogCellWithTableView:tableView];
(2)//设置子控件的值
CZMicroBlogCell *microBlog =self.microBlogs[indexPath.row];
cell.microBlog =microBlog;//它应该提供一个模型属性
(3)//返回
return cell;
// 因为没有 microBlog 属性 和microBlogCellWithTableView 类方法 所以我们要去写
//(在ViewController.h 中)
@class CZMicroBlog;
@property(nonatomic,copy) CZMicroBlog *microBlog;
+(instancetype)microBlogCellWithTableView:(UITableView *)tableView;
//(在CZMicroBlogCell.m中)实现这个类方法
// QQ项目
//1 创建自定义可重用cell的对象
+ (instancetype)messageCellWithTableView:(UITableView *)tableView
{
static NSString *reuseId = @"msg";
CZMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
if (cell == nil) {
cell = [[CZMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
}
return cell;
}
//2 创建子控件
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//时间
UILabel *timeView = [[UILabel alloc] init];
[self.contentView addSubview:timeView];
self.timeView = timeView;
//头像
UIImageView *iconView = [[UIImageView alloc] init];
[self.contentView addSubview:iconView];
self.iconView = iconView;
//聊天内容
UIButton *textView = [[UIButton alloc] init];
[self.contentView addSubview:textView];
self.textView = textView;
}
return self;
}
//3 重写属性的setter方法
- (void)setMessage:(CZMessage *)message
{
_message = message;
}
@end
//没办法算高度 所以要创建一个frame 模型
类名字 CZMessageFrame 继承 NSObject
IOS学习之——自定义的Cell (代码)
最新推荐文章于 2024-06-16 21:11:22 发布