XIB 初始化cell

2019-01-11  本文已影响17人  叩首问路梦码为生

iOS关于使用xib创建cell的3种初始化方式

2018年05月08日 18:45:11 面壁者LOGIC 阅读数:2335

<article class="baidu_pl">

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Bolted_snail/article/details/80244046

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 30;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //直接通过标识去获取cell
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"JHCELL"];
    cell.textLabel.text = [NSString stringWithFormat:@"%zd",indexPath.row];
    return cell;
}

运行效果如下:


运行效果

虽然这种方法很简单,但这种只是UITableViewCell,不是自定义,下面来看下用xib自定义的cell该如何出书啊。


#import "ViewController.h"
#import "JHCell.h"
@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    //设置行高
    self.tableView.rowHeight = 100;
    //tableView注册要复用的cell
    [self.tableView registerNib:[UINib nibWithNibName:@"JHCell" bundle:nil] forCellReuseIdentifier:@"JHCELL"];
    //不使用nib时注册cell
    //[self.tableView registerClass:[JHCell class] forCellReuseIdentifier:@"JHCELL"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 50;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //获取绑定标识的cell
    JHCell * cell = [tableView dequeueReusableCellWithIdentifier:@"JHCELL"];
    cell.indexL.text = [NSString stringWithFormat:@"%zd",indexPath.row];
    return cell;
}

结果展示:


结果展示
#import <UIKit/UIKit.h>

@interface JHCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *indexL;
+(instancetype)xibTableViewJHCell;
@end

#import "JHCell.h"

@implementation JHCell

+(instancetype)xibTableViewJHCell{
    NSLog(@"%s",__func__);
    return (JHCell *)[[[NSBundle mainBundle] loadNibNamed:@"JHCell" owner:nil options:nil] lastObject];
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        //初始化子类
        NSLog(@"%s",__func__);
    }
    return self;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    //设置子类
     NSLog(@"%s",__func__);
}
@end

//控制器
#import "ViewController.h"
#import "JHCell.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置行高
    self.tableView.rowHeight = 100;
    //不使用nib时注册cell
//    [self.tableView registerClass:[JHCell class] forCellReuseIdentifier:@"JHCELL"];

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 50;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //获取绑定标识的cell
    JHCell * cell = [tableView dequeueReusableCellWithIdentifier:@"JHCELL"];
    if (cell == nil) {
        cell = [JHCell xibTableViewJHCell];
    }
    cell.indexL.text = [NSString stringWithFormat:@"%zd",indexPath.row];
    return cell;
}
@end

效果同方案2。

</article>

上一篇 下一篇

猜你喜欢

热点阅读