在 -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
Animal *animal = _animalArray[indexPath.row];
NSLog(@"从数组中取出的对象: %@", animal);
BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([animal class])];
NSLog(@"从重用池中取出的cell对象: %@, 重用标识为: %@", cell, NSStringFromClass([animal class]));
if (nil == cell) {
cell = [CellFactory cellForAnimal:animal];
}
cell.animal = animal;
return cell;
}
中写一种新的cell方法.
写一个类继承NSObject, 名字Animal, 在.h中写个属性
@property(nonatomic, copy)NSString *name;
写一个自定义cell(BaseTableViewCell)的类,在.h中写 @class Animal;
在写一个属性 @property(nonatomic, strong)Animal *animal;
建立类(Dog),继承Animal,在.h中写属性
@property(nonatomic, copy)NSString *birdKind;
在写一个自定义cell(CellForDog),继承BaseTableViewCell,在.m中写
import “CellForDog.h”
import “Dog.h”
@interface CellForDog ()
@property(nonatomic, retain)UILabel *dogLabel;
@end
@implementation CellForDog
(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.dogLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
[self.contentView addSubview:self.dogLabel];
self.dogLabel.backgroundColor = [UIColor greenColor];}
return self;
}
-(void)setAnimal:(Animal *)animal{[super setAnimal:animal];
Dog dog = (Dog )animal;
self.dogLabel.t=[NSString stringWithFormat:@”name:%@,King:%@”,dog.name,dog.dogKind];
}