自定义UITableViewCell(2) 多个自定义cell

来源:互联网 发布:mac 从远程拷贝文件夹 编辑:程序博客网 时间:2024/06/09 18:00
1. 创建自定义的cell这里注意一定要添加到self.contentView上- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];    if (self) {        self.labelName = [[UILabel alloc] initWithFrame:(CGRectMake(10, 0, 120, 40))];        self.labelName.backgroundColor = [UIColor orangeColor];        [self.contentView addSubview:self.labelName];        [self.labelName release];                self.labelGender = [[UILabel alloc] initWithFrame:(CGRectMake(120 + 40 + 10, 0, 120, 40))];        self.labelGender.backgroundColor = [UIColor yellowColor];        [self.contentView addSubview:self.labelGender];        [self.labelGender release];    }    return self;}2. 在model的.h文件中建立枚举值// 枚举:// NSInteger枚举值的类型// CellType 枚举的名字(类型)typedef NS_ENUM(NSInteger, CellType) {    // 枚举值    CellTypeOne,    CellTypeTwo};@interface CellModel : NSObject@property (nonatomic, retain) NSString *name;@property (nonatomic, retain) NSString *gender;// 创建一个标签type控制显示哪种cell// type1显示第一种, type2显示第二种@property (nonatomic, assign) CellType type;@end3. 在创建数据的时候加入判断, 一定要在数据添加到数组中之前进行判断- (void)makeData {    self.dataArr = [NSMutableArray arrayWithCapacity:1];    for (int i = 0; i < 10; i++) {        CellModel *model = [[CellModel alloc] init];        model.name = [NSString stringWithFormat:@"姓名:%d", i];        model.gender = [NSString stringWithFormat:@"性别:%d", i];               // 给一个初始化的类型, 这里是偶数就用CellTypeOne这cell, 是奇数就用CellTypeTwo这个cell, 这里可以根据需求加入判断, 用一个界面实现展示不同的cell        if (i % 2 == 0) {            model.type = CellTypeOne;        } else {            model.type = CellTypeTwo;        }               [self.dataArr addObject:model];    }}4. cell的创建, 这里一定要先判断是那个cell 再创建, 避免浪费内存- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    CellModel *model = self.dataArr[indexPath.row];       if (model.type == CellTypeOne) {        static NSString *identifer = @"myCell";        MyTableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:identifer];        if (myCell == nil) {            myCell = [[[MyTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:identifer] autorelease];        }               myCell.labelName.text = model.name;        myCell.labelGender.text = model.gender;        return myCell;    } else {        static NSString *identifer2 = @"secondCell";        SecondTableViewCell *secondCell = [tableView dequeueReusableCellWithIdentifier:identifer2];        if (secondCell == nil) {            secondCell = [[[SecondTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifer2] autorelease];        }        secondCell.labelName.text = model.name;        secondCell.labelGender.text = model.gender;               return secondCell;    }}5. 根据显示的不同的内容展示不同的高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    // 这里cellOfHeight只是设置高度的自定义函数, 在自定义cell中的.m文件中建立的    CGFloat height = [MyTableViewCell cellOfHeight];    CGFloat height2 = [SecondTableViewCell cellOfHeight];    CellModel *model = self.dataArr[indexPath.row];    if (model.type == CellTypeOne) {        return height;    } else {        return height2;    }}

0 0