第一:先来看看实际长成什么样吧
第二:来看看类的结构
1是模型数据,2是xib的三个文件,3是控制器
第三:来看看2中的.m文件
#import "HYLWeiBoCell.h"
@interface HYLWeiBoCell ()
@end
@implementation HYLWeiBoCell
+(instancetype) weiBoCellWithTableView:(UITableView *)tableView{
//缓存中是否有
HYLWeiBoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"weibo"];
//没有怎么办,不管加载还是什么的送一个给它就可以了
if (cell==nil) {
cell=[[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([HYLWeiBoCell class]) owner:nil options:nil]firstObject];
}
return cell;
}
@end
上述代码主要是实例化一个重用cell,重用id 设置见图
再看下xib
xib中涉及了一些自动布局的知识。
第三:来看下控制器
#import "HYLTableViewController.h"
#import "HYLWeiBoCell.h"
@interface HYLTableViewController ()
@end
@implementation HYLTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight=90;
}
#pragma mark - setter/getter
#pragma mark - UITableViewDataSource
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//创建cell
HYLWeiBoCell *cell=[HYLWeiBoCell weiBoCellWithTableView:tableView];
//向cell中添加数据
//返回cell
return cell;
}
#pragma mark - UITableViewDelegate
@end
这是数据源那一块的知识,不作说明,在下面图片中:
在上面图片中123三个步骤,已经决定我们封装的方向应如何写了。
最后,整个源码的地址,请点此
下一篇将在些篇代码的基础上完成博客的。