UITableView-微博-cell等高的实现

本文介绍了iOS开发中微博单元格(HYLWeiBoCell)的实例化过程,包括使用xib文件进行界面搭建、自动布局应用及UITableView数据源的配置。

第一:先来看看实际长成什么样吧

    

第二:来看看类的结构

    

    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三个步骤,已经决定我们封装的方向应如何写了。

 最后,整个源码的地址,请点此

        下一篇将在些篇代码的基础上完成博客的。

转载于:https://my.oschina.net/HYLApple/blog/684639

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值