多种cell混合使用

有时候我们会碰到一个tableView上有多种cell,这个时候就需要定义多种cell,根据条件判断,当满足某个条件的时候选择某个cell

先看plist文件:

Person.h

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface Person : NSObject
 4 
 5 @property (nonatomic, copy) NSString *name;
 6 @property (nonatomic, copy) NSString *status;
 7 @property (nonatomic, copy) NSString *imageName;
 8 @property (nonatomic, copy) NSString *type;
 9 
10 @end

LabelCell.h

 1 #import <UIKit/UIKit.h>
 2 
 3 @interface LabelCell : UITableViewCell
 4 
 5 // nameLabel
 6 @property (nonatomic, strong) UILabel *nameLabel;
 7 
 8 // statusLabel
 9 @property (nonatomic, strong) UILabel *statusLabel;
10 
11 @end

LabelCell.m

 1 #import "LabelCell.h"
 2 
 3 @implementation LabelCell
 4 
 5 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
 6     
 7     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
 8     
 9     if (self) {
10         [self add];
11     }
12     return self;
13 }
14 
15 
16 - (void)add {
17     
18     self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 10, 100, 40)];
19 //    self.nameLabel.backgroundColor = [UIColor redColor];
20     self.nameLabel.font = [UIFont fontWithName:@"Helvetica" size:22];
21 //    NSLog(@"%@", [UIFont familyNames]);
22     
23     [self.contentView addSubview:self.nameLabel];
24     
25     
26     
27     self.statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(self.nameLabel.frame) + 20, 200, 40)];
28 //    self.statusLabel.backgroundColor = [UIColor greenColor];
29     self.statusLabel.font = [UIFont systemFontOfSize:16];
30     
31     [self.contentView addSubview:self.statusLabel];
32 }
33 
34 @end

ImageViewCell.h

1 #import <UIKit/UIKit.h>
2 
3 @interface ImageViewCell : UITableViewCell
4 
5 @property (nonatomic, strong) UIImageView *myImageView;
6 @property (nonatomic, strong) UILabel *nameLabel;
7 @property (nonatomic, strong) UILabel *statusLabel;
8 
9 @end

ImageViewCell.m

 1 #import "ImageViewCell.h"
 2 
 3 @implementation ImageViewCell
 4 
 5 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
 6     
 7     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
 8     if (self) {
 9         [self add];
10     }
11     return self;
12 }
13 
14 - (void)add {
15     
16     self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 10, 100, 40)];
17 //    self.nameLabel.backgroundColor = [UIColor orangeColor];
18     self.nameLabel.font = [UIFont fontWithName:@"Helvetica" size:22];
19     
20     [self.contentView addSubview:self.nameLabel];
21     
22     
23     self.statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(self.nameLabel.frame) + 20, 100, 40)];
24     self.statusLabel.numberOfLines = 0;
25 //    self.statusLabel.backgroundColor = [UIColor blueColor];
26     self.statusLabel.font = [UIFont systemFontOfSize:16];
27     
28     [self.contentView addSubview:self.statusLabel];
29     
30     
31     self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.nameLabel.frame) + 150, 10, 100, 100)];
32 //    self.myImageView.backgroundColor = [UIColor purpleColor];
33     
34     [self.contentView addSubview:self.myImageView];
35     
36 }
37 @end

RootTableViewController.m

  1 #import "RootTableViewController.h"
  2 #import "Person.h"
  3 #import "LabelCell.h"
  4 #import "ImageViewCell.h"
  5 
  6 @interface RootTableViewController ()
  7 
  8 // 声明大数组存放所有数据
  9 @property (nonatomic, strong) NSMutableArray *allPersonArray;
 10 
 11 @end
 12 
 13 @implementation RootTableViewController
 14 
 15 // 懒加载
 16 - (NSMutableArray *)allPersonArray {
 17     
 18     if (_allPersonArray == nil) {
 19         _allPersonArray = [NSMutableArray array];
 20     }
 21     return _allPersonArray;
 22 }
 23 
 24 
 25 - (void)viewDidLoad {
 26     [super viewDidLoad];
 27     
 28     
 29     self.title = @"多种cell";
 30     self.navigationController.navigationBar.barTintColor = [UIColor lightGrayColor];
 31     
 32     
 33     // 读取数据
 34     [self handleData];
 35     
 36     
 37     // 第一步:注册cell,有几个自定义cell就注册几个
 38     [self.tableView registerClass:[LabelCell class] forCellReuseIdentifier:@"labelCell"];
 39     [self.tableView registerClass:[ImageViewCell class] forCellReuseIdentifier:@"imageCell"];
 40     
 41 }
 42 
 43 
 44 // 读取数据
 45 - (void)handleData {
 46     
 47     // 1. 获取文件路径
 48     NSString *path = [[NSBundle mainBundle] pathForResource:@"person.plist" ofType:nil];
 49     
 50     // 2. 根据路径读取数据
 51     NSArray *dataArray = [NSArray arrayWithContentsOfFile:path];
 52     //NSLog(@"%@", dataArray);
 53     
 54     // 3.将数据转为model对象
 55     for (NSDictionary *dict in dataArray) {
 56         
 57         // 3.1 创建model对象
 58         Person *person = [[Person alloc] init];
 59         
 60         // 3.2 使用KVC赋值
 61         [person setValuesForKeysWithDictionary:dict];
 62         
 63         // 3.3 将model对象存放到大数组中
 64         [self.allPersonArray addObject:person];
 65         
 66     }
 67     //NSLog(@"%@", self.allPersonArray);
 68 }
 69 
 70 
 71 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 72     
 73     return 1;
 74 }
 75 
 76 
 77 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 78 
 79     return self.allPersonArray.count;
 80 }
 81 
 82 
 83 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 84     
 85     // 第二步:重用cell
 86     // 获取数据model
 87     Person *person = self.allPersonArray[indexPath.row];
 88     
 89     // 判断数据类型,重用相应的cell对象
 90     if ([person.type isEqualToString:@"1"]) {
 91         LabelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"labelCell" forIndexPath:indexPath];
 92         
 93         cell.nameLabel.text = person.name;
 94         cell.statusLabel.text = person.status;
 95         
 96         return cell;
 97     }
 98     
 99     ImageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imageCell" forIndexPath:indexPath];
100     
101     cell.nameLabel.text = person.name;
102     cell.statusLabel.text = person.status;
103     cell.myImageView.image = [UIImage imageNamed:person.imageName];
104     
105     return cell;
106 }
107 
108 
109 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
110     
111     return 120;
112 }
113 
114 @end

 

转载于:https://www.cnblogs.com/zhizunbao/p/5422536.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值