1.RootTableViewController.m
1 #import "RootTableViewController.h" 2 #import "FirstTableViewCell.h" 3 #import "MyTableViewCell.h" 4 #import "FirstModel.h" 5 #import "MyModel.h" 6 @interface RootTableViewController () 7 @property (nonatomic,strong) NSMutableArray *saveDataArray; 8 @end 9 10 @implementation RootTableViewController 11 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 //注册cell(注册cell的时候也是初始化cell) 15 //[self.tableView registerClass:[UITableView class] forCellReuseIdentifier:@"cell"]; 16 [self.tableView registerClass:[FirstTableViewCell class] forCellReuseIdentifier:@"cell"]; 17 //注册cell 18 [self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"MyTableViewCell"]; 19 //隐藏分割线 20 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 21 22 //使用KVC赋值Model类 23 NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"firstImage",@"firstName",@"secondImage",@"secondName",@"thirdImage",@"thirdName" ,nil]; 24 //初始化 25 self.saveDataArray = [[NSMutableArray alloc] init]; 26 //创建一个Model类 27 FirstModel *firstModel = [[FirstModel alloc] init]; 28 [firstModel setValuesForKeysWithDictionary:dic]; 29 [self.saveDataArray addObject:firstModel]; 30 31 //使用KVC添加第二个model类 32 NSMutableDictionary *dicTwo = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"firstImage",@"picName",@"你们要加油哦",@"title",@"每天要多敲好多行代码",@"detail",@"4000",@"common", nil]; 33 //创建一个Model类 34 MyModel *secondModel = [[MyModel alloc] init]; 35 [secondModel setValuesForKeysWithDictionary:dicTwo]; 36 [self.saveDataArray addObject:secondModel]; 37 38 } 39 40 - (void)didReceiveMemoryWarning { 41 [super didReceiveMemoryWarning]; 42 // Dispose of any resources that can be recreated. 43 } 44 45 #pragma mark - Table view data source 46 47 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 48 49 return 1; 50 } 51 52 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 53 return self.saveDataArray.count; 54 } 55 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 56 { 57 return 100; 58 } 59 60 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 61 //从数组中取出model对象 62 id object = [self.saveDataArray objectAtIndex:indexPath.row]; 63 // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 64 //需求:两行分别显示不同类型的cell,此时需要根据判断进行cell的设置 65 if (indexPath.row == 0) { 66 FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 67 cell.textLabel.text = [NSString stringWithFormat:@"row:%ld",indexPath.row]; 68 cell.model = object; 69 return cell; 70 71 }else{ 72 MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyTableViewCell"]; 73 cell.model = object; 74 return cell; 75 } 76 77 78 79 }
2.FirstTableViewCell.h
1 #import <UIKit/UIKit.h> 2 @class FirstModel; 3 //自定义第一种cell 4 @interface FirstTableViewCell : UITableViewCell 5 @property(nonatomic,strong) UIImageView *firstImageView; 6 @property(nonatomic,strong) UIImageView *secondImageView; 7 @property(nonatomic,strong) UIImageView *thirdImageView; 8 //设置数据model 9 @property(nonatomic,strong) FirstModel *model; 10 @end
3.FirstTableViewCell.m
1 #import "FirstTableViewCell.h" 2 #import "FirstModel.h" 3 @implementation FirstTableViewCell 4 5 - (void)awakeFromNib { 6 // Initialization code 7 } 8 -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 9 { 10 self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 11 if (self) { 12 //添加相应的控件 13 self.firstImageView = [[UIImageView alloc] initWithFrame:CGRectZero]; 14 [self.contentView addSubview:self.firstImageView]; 15 16 self.secondImageView = [[UIImageView alloc] initWithFrame:CGRectZero]; 17 [self.contentView addSubview:self.secondImageView]; 18 19 self.thirdImageView = [[UIImageView alloc] initWithFrame:CGRectZero]; 20 [self.contentView addSubview:self.thirdImageView]; 21 } 22 return self; 23 } 24 //设置控件相对应的frame 25 -(void)layoutSubviews 26 { 27 self.firstImageView.frame = CGRectMake(10, 20, 60, 60); 28 self.firstImageView.image = [UIImage imageNamed:self.model.firstName]; 29 self.secondImageView.frame = CGRectMake(80, 20, 60, 60); 30 self.secondImageView.image = [UIImage imageNamed:self.model.secondName]; 31 self.thirdImageView.frame = CGRectMake(150, 20, 60, 60); 32 self.thirdImageView.image = [UIImage imageNamed:self.model.thirdName]; 33 } 34 - (void)setSelected:(BOOL)selected animated:(BOOL)animated { 35 [super setSelected:selected animated:animated]; 36 37 // Configure the view for the selected state 38 } 39 40 @end
4.FirstModel.h
1 #import <Foundation/Foundation.h> 2 3 @interface FirstModel : NSObject 4 @property (nonatomic,strong) NSString *firstName; 5 @property (nonatomic,strong) NSString *secondName; 6 @property (nonatomic,strong) NSString *thirdName; 7 @end
5.firstmodel.m
1 #import "FirstModel.h" 2 3 @implementation FirstModel 4 //复习KVC的相关内容,这个方式是防止找不到与之对应的key的时候造成程序crash掉 5 -(void)setValue:(id)value forUndefinedKey:(NSString *)key 6 { 7 8 } 9 @end
6.MyTableViewCell.h
1 #import <UIKit/UIKit.h> 2 @class MyModel; 3 @interface MyTableViewCell : UITableViewCell 4 @property (nonatomic,strong) UIImageView *showImageView; 5 @property (nonatomic,strong) UILabel *titleLable; 6 @property (nonatomic,strong) UILabel *detailLable; 7 @property (nonatomic,strong) UILabel *responceLable; 8 @property (nonatomic,strong) MyModel *model; 9 @end
7.MyTableViewCell.m
1 #import "MyTableViewCell.h" 2 #import "MyModel.h" 3 @implementation MyTableViewCell 4 5 - (void)awakeFromNib { 6 // Initialization code 7 } 8 -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 9 { 10 self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 11 if (self) { 12 self.showImageView = [[UIImageView alloc] initWithFrame:CGRectZero]; 13 [self.contentView addSubview:self.showImageView]; 14 15 16 self.titleLable = [[UILabel alloc] initWithFrame:CGRectZero]; 17 [self.contentView addSubview:self.titleLable]; 18 19 20 self.detailLable = [[UILabel alloc] initWithFrame:CGRectZero]; 21 [self.contentView addSubview:self.detailLable]; 22 23 self.responceLable = [[UILabel alloc] initWithFrame:CGRectZero]; 24 [self.contentView addSubview:self.responceLable]; 25 26 27 } 28 return self; 29 } 30 -(void)layoutSubviews 31 { 32 self.showImageView.frame = CGRectMake(10, 20, 60, 60); 33 //self.showImageView.image = [UIImage imageNamed:@"firstImage"]; 34 35 36 self.titleLable.frame = CGRectMake(self.showImageView.bounds.origin.x + self.showImageView.bounds.size.width + 10 ,20, self.contentView.bounds.size.width - self.showImageView.bounds.size.width - 20, 30); 37 self.titleLable.backgroundColor = [UIColor cyanColor]; 38 [self.titleLable setFont:[UIFont systemFontOfSize:14]]; 39 40 41 self.detailLable.frame = CGRectMake(self.titleLable.frame.origin.x, self.titleLable.frame.origin.y + self.titleLable.frame.size.height + 5, self.titleLable.frame.size.width, 40); 42 self.detailLable.backgroundColor = [UIColor redColor]; 43 self.detailLable.font = [UIFont systemFontOfSize:14]; 44 45 46 self.responceLable.frame = CGRectMake(self.contentView.bounds.size.width - 80, 100, 40, 20); 47 self.responceLable.font = [UIFont systemFontOfSize:12]; 48 self.responceLable.backgroundColor = [UIColor redColor]; 49 50 self.showImageView.image = [UIImage imageNamed:self.model.picName]; 51 self.titleLable.text = self.model.title; 52 self.detailLable.text = self.model.detail; 53 self.responceLable.text = self.model.common; 54 } 55 - (void)setSelected:(BOOL)selected animated:(BOOL)animated { 56 [super setSelected:selected animated:animated]; 57 58 // Configure the view for the selected state 59 } 60 61 @end
8.MyModel.h
1 #import <Foundation/Foundation.h> 2 3 @interface MyModel : NSObject 4 @property(nonatomic,strong)NSString *picName; 5 @property(nonatomic,strong) NSString *title; 6 @property (nonatomic,strong) NSString *detail; 7 @property (nonatomic,strong) NSString *common; 8 @end
时间: 2024-09-29 21:04:07