有时候我们会碰到一个tableView上有多种cell,这个时候就需要定义多种cell,根据条件判断,当满足某个条件的时候选择某个cell
先看plist文件:
Person.h
#import <Foundation/Foundation.h> @interface Person : NSObject @property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *status;
@property (nonatomic, copy) NSString *imageName;
@property (nonatomic, copy) NSString *type; @end
LabelCell.h
#import <UIKit/UIKit.h> @interface LabelCell : UITableViewCell // nameLabel
@property (nonatomic, strong) UILabel *nameLabel; // statusLabel
@property (nonatomic, strong) UILabel *statusLabel; @end
LabelCell.m
#import "LabelCell.h" @implementation LabelCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) {
[self add];
}
return self;
} - (void)add { self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
// self.nameLabel.backgroundColor = [UIColor redColor];
self.nameLabel.font = [UIFont fontWithName:@"Helvetica" size:];
// NSLog(@"%@", [UIFont familyNames]); [self.contentView addSubview:self.nameLabel]; self.statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(, CGRectGetMaxY(self.nameLabel.frame) + , , )];
// self.statusLabel.backgroundColor = [UIColor greenColor];
self.statusLabel.font = [UIFont systemFontOfSize:]; [self.contentView addSubview:self.statusLabel];
} @end
ImageViewCell.h
#import <UIKit/UIKit.h> @interface ImageViewCell : UITableViewCell @property (nonatomic, strong) UIImageView *myImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *statusLabel; @end
ImageViewCell.m
#import "ImageViewCell.h" @implementation ImageViewCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self add];
}
return self;
} - (void)add { self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
// self.nameLabel.backgroundColor = [UIColor orangeColor];
self.nameLabel.font = [UIFont fontWithName:@"Helvetica" size:]; [self.contentView addSubview:self.nameLabel]; self.statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(, CGRectGetMaxY(self.nameLabel.frame) + , , )];
self.statusLabel.numberOfLines = ;
// self.statusLabel.backgroundColor = [UIColor blueColor];
self.statusLabel.font = [UIFont systemFontOfSize:]; [self.contentView addSubview:self.statusLabel]; self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.nameLabel.frame) + , , , )];
// self.myImageView.backgroundColor = [UIColor purpleColor]; [self.contentView addSubview:self.myImageView]; }
@end
RootTableViewController.m
#import "RootTableViewController.h"
#import "Person.h"
#import "LabelCell.h"
#import "ImageViewCell.h" @interface RootTableViewController () // 声明大数组存放所有数据
@property (nonatomic, strong) NSMutableArray *allPersonArray; @end @implementation RootTableViewController // 懒加载
- (NSMutableArray *)allPersonArray { if (_allPersonArray == nil) {
_allPersonArray = [NSMutableArray array];
}
return _allPersonArray;
} - (void)viewDidLoad {
[super viewDidLoad]; self.title = @"多种cell";
self.navigationController.navigationBar.barTintColor = [UIColor lightGrayColor]; // 读取数据
[self handleData]; // 第一步:注册cell,有几个自定义cell就注册几个
[self.tableView registerClass:[LabelCell class] forCellReuseIdentifier:@"labelCell"];
[self.tableView registerClass:[ImageViewCell class] forCellReuseIdentifier:@"imageCell"]; } // 读取数据
- (void)handleData { // 1. 获取文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"person.plist" ofType:nil]; // 2. 根据路径读取数据
NSArray *dataArray = [NSArray arrayWithContentsOfFile:path];
//NSLog(@"%@", dataArray); // 3.将数据转为model对象
for (NSDictionary *dict in dataArray) { // 3.1 创建model对象
Person *person = [[Person alloc] init]; // 3.2 使用KVC赋值
[person setValuesForKeysWithDictionary:dict]; // 3.3 将model对象存放到大数组中
[self.allPersonArray addObject:person]; }
//NSLog(@"%@", self.allPersonArray);
} - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.allPersonArray.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 第二步:重用cell
// 获取数据model
Person *person = self.allPersonArray[indexPath.row]; // 判断数据类型,重用相应的cell对象
if ([person.type isEqualToString:@""]) {
LabelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"labelCell" forIndexPath:indexPath]; cell.nameLabel.text = person.name;
cell.statusLabel.text = person.status; return cell;
} ImageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imageCell" forIndexPath:indexPath]; cell.nameLabel.text = person.name;
cell.statusLabel.text = person.status;
cell.myImageView.image = [UIImage imageNamed:person.imageName]; return cell;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return ;
} @end