1.自定义cell
2.多种cell 的混合使用
3.cell自适应高度
自定义cell就是创建一个UITableViewCell的子类。
把cell上的控件创建都封装在子类中,简化UIViewController中的代码
子视图控件添加到cell的contentView上
cell中的控件如何显示Model中的信息?
cell中声明一个Model类型的属性,viewController中获取到Model对象后赋值给cell的Model属性,cell中重写Model的setter方法,把Model对象中的内容重新赋值给各个控件M和V不直接进行通信,C负责M和V之间进行通信
通常我们会在tableView:cellForRowAtIndexPath:方法中根据不同的
Model来决定使用什么类型的cell每种类型的cell要定义不同的重用标识符cell重用的时候会根据重用标识从重用队列中取用哪种类型的cell
代码如下:
建一个Model
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *age;
@property(nonatomic,strong)NSString *tel;
@property(nonatomic,strong)NSString *qq;
@property(nonatomic,strong)NSString *description;
@property(nonatomic,strong)NSString *sex;
@property(nonatomic,strong)NSString *icon;
@end
#import "Student.h"
@implementation Student
//如果没有找到相应的key值走这个方法
-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
}
@end
//创建一个文件plist,用来存放数据
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>name</key>
<string>小明</string>
<key>age</key>
<string>18</string>
<key>tel</key>
<string>18888888</string>
<key>qq</key>
<string>700000</string>
<key>sex</key>
<string>女</string>
<key>description</key>
<string>装逼只是瞬间,不要脸那才是永恒</string>
<key>icon</key>
<string>lishijie.png</string>
</dict>
<dict>
<key>name</key>
<string>小馨</string>
<key>age</key>
<string>18</string>
<key>tel</key>
<string>110</string>
<key>qq</key>
<string>123456</string>
<key>sex</key>
<string>女</string>
<key>description</key>
<string>装逼只是瞬间,不要脸那才是永恒</string>
<key>icon</key>
<string>Leixin.png</string>
</dict>
</array>
</plist>
创建一个RootTableViewController
#import "RootTableViewController.h"
#import "RootTableViewCell.h"
#import "Student.h"
#import "GirlTableViewCell.h"
@interface RootTableViewController ()
//用来存储有多少个数组的
@property(nonatomic,strong)NSMutableArray *groupArray;
@end
@implementation RootTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//路径取出数据
NSString *path = [[NSBundle mainBundle] pathForResource:@"student" ofType:@"plist"];
//临时获得数据
NSArray *tempArray = [NSArray arrayWithContentsOfFile:path];
//临时分组存放
NSMutableArray *arr = [NSMutableArray array];
for (NSDictionary *dict in tempArray)
{
Student *stu = [[Student alloc] init];
//匹配stu中的key
[stu setValuesForKeysWithDictionary:dict];
[arr addObject:stu];
}
//把临时分组加入总分组中
self.groupArray = [NSMutableArray arrayWithObject:arr];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return self.groupArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.groupArray[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Student *stu = self.groupArray[indexPath.section][indexPath.row];
if ([stu.sex isEqualToString:@"男"])
{
NSString *cell_id =@"cell_boy";
RootTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
if (nil == cell) {
cell = [[RootTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_id];
}
// cell.textLabel.text = @"测试数据";
cell.nameLabel.text = [self.groupArray[indexPath.section][indexPath.row] name];
cell.telLabel.text = [self.groupArray[indexPath.section][indexPath.row] tel];
cell.descriptionLabel.text = [self.groupArray[indexPath.section][indexPath.row] description];
//定义descriptionLabel的fram
//[RootTableViewCell textHeight:stu.description]调用方法计算文字的高度
cell.descriptionLabel.frame = CGRectMake(cell.descriptionLabel.frame.origin.x, cell.descriptionLabel.frame.origin.y, cell.descriptionLabel.frame.size.width, [RootTableViewCell textHeight:stu.description]);
//计算文字高度方法二:
// //获取label 的高度
// CGRect tempRect = cell.descriptionLabel.frame;
// //temp的高度等于输入文字的高度
// tempRect.size.height = [RootTableViewCell textHeight:stu.description];
// //把temp 的fram给label
// cell.descriptionLabel.frame = tempRect;
// //
cell.descriptionLabel.numberOfLines = 0;
return cell;
}else{
NSString *cell_id = @"cell_girl";
GirlTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
if (nil == cell) {
cell = [[GirlTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_id];
}
cell.nameLabel.text = [self.groupArray[indexPath.section][indexPath.row] name];
cell.telLabel.text = [self.groupArray[indexPath.section][indexPath.row] tel];
cell.iconImage.image = [UIImage imageNamed:stu.icon];
return cell;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Student *stu = self.groupArray[indexPath.section][indexPath.row];
if ([stu.sex isEqualToString:@"男"]) {
return [RootTableViewCell cellHeight:stu.description];
}else{
return 100;
}
}
@end
//创建
//UITableViewCell
#import <UIKit/UIKit.h>
@interface RootTableViewCell : UITableViewCell
@property(nonatomic,strong)UIImageView *iconImage;
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UILabel *telLabel;
@property(nonatomic,strong)UILabel *descriptionLabel;
//类方法:一定是在创建cell显示之前,知道文字的高度
//计算cell 的高度
//类方法是创建之前
+(CGFloat)cellHeight:(NSString *)text;
//计算文字的高度
+(CGFloat)textHeight:(NSString *)aString;
@end
#import "RootTableViewCell.h"
@implementation RootTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self addAllViews];
}
return self;
}
//计算cell 的高度
+(CGFloat)cellHeight:(NSString *)text
{
//死值和文字的高度
return 20+60+10+20+[[self class] textHeight:text];
}
//计算文字的高度
+(CGFloat)textHeight:(NSString *)aString
{
NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
//计算文本里面字体的高度
CGRect textRect = [aString boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, 1000)options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dict context:nil];
//只使用它的高度,所以返回他的高度
return textRect.size.height;
}
-(void)addAllViews
{
//头像图片
self.iconImage = [[UIImageView alloc] init];
self.iconImage.frame = CGRectMake(20, 20, 60, 60);
self.iconImage.backgroundColor = [UIColor redColor];
[self.contentView addSubview:self.iconImage];
//姓名label
self.nameLabel = [[UILabel alloc] init];
self.nameLabel.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame) + 10, CGRectGetMinY(self.iconImage.frame), self.bounds.size.width - 40 - self.iconImage.frame.size.width - 10, 25);
self.nameLabel.backgroundColor= [UIColor blueColor];
[self.contentView addSubview:self.nameLabel];
//电话label
self.telLabel = [[UILabel alloc] init];
self.telLabel.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame) + 10, CGRectGetMaxY(self.nameLabel.frame) + 10, self.bounds.size.width - 40 - self.iconImage.frame.size.width - 10, 25);
self.telLabel.backgroundColor= [UIColor blueColor];
self.descriptionLabel.numberOfLines = 0;
[self.contentView addSubview:self.telLabel];
//简介label
self.descriptionLabel = [[UILabel alloc] init];
self.descriptionLabel.frame = CGRectMake(CGRectGetMinX(self.iconImage.frame), CGRectGetMaxY(self.iconImage.frame) + 10, self.bounds.size.width - 40, 80);
self.descriptionLabel.backgroundColor = [UIColor cyanColor];
[self.contentView addSubview:self.descriptionLabel];
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
//自定义cell
#import <UIKit/UIKit.h>
@interface GirlTableViewCell : UITableViewCell
@property(nonatomic,strong)UIImageView *iconImage;
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UILabel *telLabel;
@end
#import "GirlTableViewCell.h"
@implementation GirlTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self addAllViews];
}
return self;
}
-(void)addAllViews
{
//头像图片
self.iconImage = [[UIImageView alloc] init];
self.iconImage.frame = CGRectMake(20, 20, 60, 60);
self.iconImage.backgroundColor = [UIColor yellowColor];
[self.contentView addSubview:self.iconImage];
//名字label
self.nameLabel = [[UILabel alloc] init];
self.nameLabel.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame) + 10, CGRectGetMinY(self.iconImage.frame), self.bounds.size.width - 40 - self.iconImage.frame.size.width - 10, 25);
self.nameLabel.backgroundColor= [UIColor greenColor];
[self.contentView addSubview:self.nameLabel];
//电话label
self.telLabel = [[UILabel alloc] init];
self.telLabel.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame) + 10, CGRectGetMaxY(self.nameLabel.frame) + 10, self.bounds.size.width - 40 - self.iconImage.frame.size.width - 10, 25);
self.telLabel.backgroundColor= [UIColor greenColor];
[self.contentView addSubview:self.telLabel];
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end