cell的3三种重用方法

来源:互联网 发布:手机淘宝用银行卡支付 编辑:程序博客网 时间:2024/05/16 12:11

cell的重用


重用标识符可以定义为全局的

static NSString *const reuseIdentify = @“Cell”;

1.一个类(含.h,.m)

例如

RightDetailCell.h文件

RightDetailCell.m文件

//重写父类的初始化方法

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    

    self = [superinitWithStyle:UITableViewCellStyleValue1reuseIdentifier:reuseIdentifier];

    if (self) {

        self.accessoryType =UITableViewCellAccessoryDisclosureIndicator;

    }

    returnself;

}

ViewController.m 的生命周期注册重用标识符(ViewDidLoad里或其他懒加载位置)

#import "RightDetailCell.h"

[self.tableView registerClass:[RightDetailCell classforCellReuseIdentifier:@"RDCell"];

再在cellForRowAtIndexPath或者cellForItemAtIndexPath里从队列取出重用

RightDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RDCell" forIndexPath:indexPath];


2.一个带xib的类(含.h .m .xib)

NormalCell.h文件

@interface NormalCell : UITableViewCell

@property (weak,nonatomic)IBOutletUIImageView *iconIV;

@property (weak,nonatomic)IBOutletUILabel *titleLb;

@property (weak,nonatomic)IBOutletUILabel *detailLb;

NormalCell.m文件

NormalCell.xib文件





1).在xib里画出需要使用的参数(view imageView label等) 并写出cell的标志符

2).将xib的cell里面的参数连接到.h的属性中(公开的)

3).在指定controller里面根据xib注册重用标识符

#import "NormalCell.h"

[self.tableView registerNib:[UINib nibWithNibName:@"NormalCell" bundle:nilforCellReuseIdentifier:@"normalCell"];

4)再在cellForRowAtIndexPath或者cellForItemAtIndexPath里从队列取出重用

NormalCell *cell = [tableView dequeueReusableCellWithIdentifier:@"normalCell" forIndexPath:indexPath];


3.纯xib

DaRenCell.xib文件  

重用标识符DaRenCell写上





if (indexPath.row ==2) {

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"DaRenCell"];

    if (cell ==nil) {

        cell = [[NSBundlemainBundle]loadNibNamed:@"DaRenCell"owner:niloptions:nil].firstObject;

     }

     return cell;

}



//1.有多少个分组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return4;

}

//2.某个分组中有几个cell

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [@[@3,@2,@3,@1][section]integerValue];

}

//3.cell样式

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

0 0
原创粉丝点击