自定义cell
//
// RootTableViewController.h
// Lesson11_自定义cell
//
// Created by Floating_SH on 15/12/1.
// Copyright © 2015年 SH. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface RootTableViewController : UITableViewController
@end
//
// RootTableViewController.m
// Lesson11_自定义cell
//
// Created by Floating_SH on 15/12/1.
// Copyright © 2015年 SH. All rights reserved.
//
#import "RootTableViewController.h"
#import "CustomCell.h"
#import "Student.h"
#import "GirlCell.h"
@interface RootTableViewController ()
@property (strong ,nonatomic) NSMutableArray *group;
@property (strong ,nonatomic) NSMutableDictionary *dataDict;
@end
static NSString *const customCellID =@"customCellReuseIdentifier";
static NSString *const girlCellID =@"girlCellReuseIdentifier";
@implementation RootTableViewController
-(instancetype)initWithStyle:(UITableViewStyle)style{
if (self = [superinitWithStyle:style]){
//获取plist文件路径
NSString *path = [[NSBundlemainBundle]pathForResource:@"CLASS151042"ofType:@"plist"];
//根据文件创建字典
NSDictionary *dict = [NSDictionarydictionaryWithContentsOfFile:path];
//获取分组数组
self.group = [[dictallKeys] mutableCopy];
[self.groupsortUsingSelector:@selector(compare:)];//排序
//遍历字典封装Model数据
//字典开辟空间
self.dataDict = [[NSMutableDictionaryalloc]initWithCapacity:26];
//遍历
for (NSString *keyin dict) {
//临时数组
NSMutableArray *tempArray = [[NSMutableArrayalloc]initWithCapacity:20];
//遍历每个key值对应的数组
for (NSDictionary *stuDictin dict[key]) {
//KVC完成赋值
Student *student = [[Studentalloc]initWithDictionary:stuDict];
//存储在临时数组中
[tempArray addObject:student];
}
//数据加入新的字典容器
[self.dataDictsetObject:tempArray forKey:key];
}
}
return self;
}
- (void)viewDidLoad {
[superviewDidLoad];
//在375的label中12号字体所占行数
// NSString *string = @"我的爱好是打篮球。几乎每天都打,这不我又出现在篮球场上了,你知道我怎么喜欢篮球的吗?告诉你吧,以前我也没打过篮球。可我喜欢看“NBA和CBA”的比赛。那些球员非常了得,抢篮板可帅了,手一勾球便到手了,那三分球更是不在话下,他们的每一个动作都是那样的潇洒自如。我心里也渴望和他们一样当个球星。我的偶像是姚明和乔丹。瞧,我的床头还贴着他俩的照片呢!爸爸说:“既然你的偶像都是篮球明星,你也就学打篮球吧,同时也学习他们在球场上的那种拼搏精神。”于是我每天都坚持打一会篮球,渐渐地便爱上了篮球。打了几个月,我发现自己长壮了,饭量也大了许多。在学习上更爱动脑筋了,所以我更喜欢打篮球了。";
//#pragma mark -------------------自适应高度-----------------------
// //根据文本计算出高度
// //第一个参数限定了在375宽度下计算出此高度,并且高度最多为1000行
// //第三个参数限定了在12号字体下计算出来的高度
// CGRect e =[string boundingRectWithSize:CGSizeMake(375, 1000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil];
// NSLog(@"%f",e.size.height);
//
//传递的是自定义的类CustomCell
[self.tableViewregisterClass:[CustomCellclass] forCellReuseIdentifier:customCellID];
[self.tableViewregisterClass:[GirlCellclass] forCellReuseIdentifier:girlCellID];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.group.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//获取当前分组的分组名
NSString *key = [self.groupobjectAtIndex:section];
//根据当前分组名获取对应的分组数组
NSArray *array = [self.dataDictobjectForKey:key];
//返回当前数组的元素个数
return array.count;
// return [self.dataDict[self.group[section]] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//找到当前分组名
NSString *key = [self.groupobjectAtIndex:indexPath.section];
//根据分组名找到对应的分组数组
NSArray *array = self.dataDict[key];
//获取当前cell对应的model对象
Student *student = [array objectAtIndex:indexPath.row];
//计算出爱好的高度
// CGFloat height = [self calculateHeightWithString:student.hobby];
// //根据model对象的属性来返回不同的cell
// if ([student.gender isEqualToString:@"女"]) {
// GirlCell *cell = [tableView dequeueReusableCellWithIdentifier:girlCellID forIndexPath:indexPath];
//
// cell.student = student;
//
// return cell;
// }
CustomCell *cell = [tableViewdequeueReusableCellWithIdentifier:customCellIDforIndexPath:indexPath];
//cell赋值
cell.student = student;
// CGRect labelFrame = cell.hobbyLabel.frame;
// labelFrame.size.height = height;
// cell.hobbyLabel.frame = labelFrame;
// //cell赋值
cell.backImgView.image = [UIImage imageNamed:student.picture];
// cell.imgView.image = [UIImage imageNamed:student.picture];
// cell.nameLabel.text= [NSString stringWithFormat:@"姓名 : %@",student.name];
// cell.genderLabel.text = [NSString stringWithFormat:@"性别 : %@",student.gender];
// cell.phoneNumberLabel.text = [NSString stringWithFormat:@"电话 : %@",student.phoneNumber];
// cell.hobbyLabel.text = student.hobby;
//颜色随机的时候只要能随机到0~1之间的数就可以
//不一定要先对256取余再除以255.0,只要随机出来的值在0~1之间即可
//写成 % 256 / 255.0的原因是方便和视觉设计师给定的数值对应起来,处理成IOS支持的颜色
CGFloat red = arc4random() % 1000 / 999.0;
CGFloat green = arc4random() % 1000 / 999.0;
CGFloat blue = arc4random() % 1000 / 999.0;
cell.contentView.backgroundColor = [UIColorcolorWithRed:red green:greenblue:blue alpha:1];
return cell;
}
#pragma mark---------------------自适应内容-----------------------
//行高(先绘制空间在设置cell)
#define kGap (kScreenWidth / 18)
#define kImgHeight (kScreenWidth / 3 * 4 / 3)
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//找到对应的分组名
NSString *key =self.group[indexPath.section];
//根据分组名找到对应的数组
NSArray *array = self.dataDict[key];
//找到对应的model对象
Student *student = array[indexPath.row];
// //计算需要自适应的内容
// CGFloat dynamicHeight = [self calculateHeightWithString:student.hobby];
//
// //固定高度
// CGFloat staticHeight = kImgHeight + 3 * kGap;
//
//
// return dynamicHeight + staticHeight;
//使用类方法来构造,返回值
return [CustomCellcalculateHeightWith:student];
}
#pragma mark--------------------↑自适应内容↑----------------------
计算字符串所占高度的方法
//-(CGFloat)calculateHeightWithString:(NSString *)string{
//
// CGRect rect = [string boundingRectWithSize:CGSizeMake(kScreenWidth - 2 * kGap, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
// return rect.size.height;
//}
//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//
// return @"通讯录";
//}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// GirlCell.h
// Lesson11_自定义cell
//
// Created by Floating_SH on 15/12/1.
// Copyright © 2015年 SH. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "Student.h"
@interface GirlCell :UITableViewCell
@property (strong ,nonatomic ) UIImageView *backImgView;
@property (strong ,nonatomic ) UIImageView *imgView;
@property (strong ,nonatomic ) UILabel *nameLabel;
@property (strong ,nonatomic ) UILabel *phoneNumberLabel;
@property (strong ,nonatomic ) UILabel *genderLabel;
@property (strong ,nonatomic ) UILabel *hobbyLabel;
@property (strong ,nonatomic )Student *student;
@end
//
// GirlCell.m
// Lesson11_自定义cell
//
// Created by Floating_SH on 15/12/1.
// Copyright © 2015年 SH. All rights reserved.
//
#import "GirlCell.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
@implementation GirlCell
- (void)setStudent:(Student *)student{
if(_student != student){
_student = nil;
_student = student;
}
self.backImgView.image = [UIImageimageNamed:student.picture];
self.imgView.image = [UIImageimageNamed:student.picture];
self.nameLabel.text = student.name;
self.genderLabel.text = student.gender;
self.phoneNumberLabel.text = student.phoneNumber;
self.hobbyLabel.text = student.hobby;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if(self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier]){
[self drawView];
}
return self;
}
#define kGap (kScreenWidth / 18)
-(void)drawView{
self.backImgView = [[UIImageViewalloc]initWithFrame:CGRectMake(kGap,kGap, kScreenWidth -2 * kGap, 320 - 2 * kGap)];
self.backImgView.image = [UIImageimageNamed:@"placeholder.jpg"];
self.backImgView.layer.cornerRadius = 7;
self.backImgView.layer.masksToBounds = YES;
[self.contentViewaddSubview:self.backImgView];
//模糊效果视图
UIBlurEffect *blur = [UIBlurEffecteffectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *visualEffect = [[UIVisualEffectViewalloc]initWithFrame:self.backImgView.bounds];
visualEffect.effect = blur;
[self.backImgViewaddSubview:visualEffect];
#define kNameWidth self.backImgView.bounds.size.width / 2
#define kNameHeight self.backImgView.bounds.size.height / 5
self.nameLabel = [[UILabelalloc]initWithFrame:CGRectMake(kGap,kGap, kNameWidth,kNameHeight)];
self.nameLabel.text =@"王军";
[self.backImgViewaddSubview:self.nameLabel];
#define kGenderWidth (self.backImgView.bounds.size.width / 2 - 3 * kGap)
self.genderLabel = [[UILabelalloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.nameLabel.frame) + kGap, kGap, kGenderWidth,kNameHeight)];
self.genderLabel.text =@"女";
[self.backImgViewaddSubview:self.genderLabel];
/***********************---------***************************/
UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(kGap,CGRectGetMaxY(self.nameLabel.frame) +kGap, self.backImgView.bounds.size.width -2 * kGap, 2)];
view.backgroundColor = [UIColorcyanColor];
[self.backImgViewaddSubview:view];
#define kImgWidth kNameWidth
#define kImgHeight (self.backImgView.bounds.size.height - 4 * kGap - kNameHeight)
self.imgView = [[UIImageViewalloc]initWithFrame:CGRectMake(kGap,CGRectGetMaxY(view.frame) +kGap, self.nameLabel.bounds.size.width,kImgHeight)];
self.imgView.image = [UIImageimageNamed:@"placeholder.jpg"];
[self.backImgViewaddSubview:self.imgView];
#define kHobbyWidth kGenderWidth
#define kHobbyHeight kImgHeight
self.hobbyLabel = [[UILabelalloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.imgView.frame) + kGap, CGRectGetMaxY(self.genderLabel.frame) + kGap, kHobbyWidth, kHobbyHeight)];
self.hobbyLabel.text = @"吾至爱汝,即此爱汝一念,使吾勇于就死也";
self.hobbyLabel.numberOfLines =10;
[self.backImgViewaddSubview:self.hobbyLabel];
}
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selectedanimated:animated];
// Configure the view for the selected state
}
@end
//
// CustomCell.h
// Lesson11_自定义cell
//
// Created by Floating_SH on 15/12/1.
// Copyright © 2015年 SH. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "Student.h"
@interface CustomCell :UITableViewCell
@property (strong ,nonatomic) UIImageView *backImgView;
@property (strong ,nonatomic) UIImageView *imgView;
@property (strong ,nonatomic) UILabel *nameLabel;
@property (strong ,nonatomic) UILabel *genderLabel;
@property (strong ,nonatomic) UILabel *phoneNumberLabel;
@property (strong ,nonatomic) UILabel *hobbyLabel;
@property (strong ,nonatomic) Student *student;
//计算高度
+ (CGFloat)calculateHeightWith:(Student *)student;
@end
//
// CustomCell.m
// Lesson11_自定义cell
//
// Created by Floating_SH on 15/12/1.
// Copyright © 2015年 SH. All rights reserved.
//
#import "CustomCell.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kGap (kScreenWidth / 18)
#define kImgWidth (kScreenWidth / 3)
#define kImgHeight (kScreenWidth / 3 * 4 / 3)
#define kLabelWidth (kScreenWidth / 2)
#define kLabelHeight (kImgHeight - 2 * kGap) /4
@implementation CustomCell
#pragma mark---------------------自适应内容-----------------------
//计算高度
+ (CGFloat)calculateHeightWith:(Student *)student{
CGFloat staticHeight = kImgHeight + 3 * kGap;
CGFloat dynamicHeight = [selfhobbyLabelHeight:student];
return staticHeight + dynamicHeight;
}
#pragma mark--------------------↑自适应内容↑----------------------
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if(self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier]){
[self drawView];
}
return self;
}
- (void)setStudent:(Student *)student{
if(_student != student){
_student = nil;
_student = student;
}
self.imgView.image = [UIImageimageNamed:student.picture];
self.nameLabel.text = student.name;
self.genderLabel.text = student.gender;
self.phoneNumberLabel.text = student.phoneNumber;
self.hobbyLabel.text = student.hobby;
#pragma mark---------------------自适应内容-----------------------
//使用计算出的结果重新调整hobbyLabel的frame
CGRect hobbyFrame = self.hobbyLabel.frame;
hobbyFrame.size.height = [[selfclass] hobbyLabelHeight:student];
//hobbyFrame.size.height = [CustomCell hobbyLabelHeight:student];//与上方一样
self.hobbyLabel.frame = hobbyFrame;
#pragma mark--------------------↑自适应内容↑----------------------
}
#pragma mark---------------------自适应内容-----------------------
//计算hobby的高度
+ (CGFloat)hobbyLabelHeight:(Student *)student{
CGRect rect = [student.hobbyboundingRectWithSize:CGSizeMake(kScreenWidth -2 * kGap, 1000)options:NSStringDrawingUsesLineFragmentOriginattributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:17]}context:nil];
return rect.size.height;
}
#pragma mark--------------------↑自适应内容↑----------------------
-(void)drawView{
self.backImgView = [[UIImageViewalloc]initWithFrame:CGRectMake(kGap,kGap, kScreenWidth -2 * kGap, 320 - 2 * kGap)];
self.backImgView.image = [UIImageimageNamed:@"placeholder.jpg"];
// self.backImgView.layer.cornerRadius = 7;
// self.backImgView.layer.masksToBounds = YES;
// [self.contentView addSubview:self.backImgView];
// //模糊效果
// UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
// UIVisualEffectView *visualEffect = [[UIVisualEffectView alloc]initWithFrame:self.backImgView.bounds];
// visualEffect.effect = blur;
// [self.contentView addSubview:visualEffect];
self.imgView = [[UIImageViewalloc]initWithFrame:CGRectMake(kGap,kGap, kImgWidth, kImgWidth)];
self.imgView.image = [UIImageimageNamed:@"placeholder.jpg"];
self.imgView.layer.cornerRadius = kImgWidth/2;
self.imgView.layer.masksToBounds = YES;
[self.contentViewaddSubview:self.imgView];
self.nameLabel = [[UILabelalloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.imgView.frame) + kGap, kGap, kLabelWidth,kLabelHeight)];
self.nameLabel.text = @"姓名 :sh";
[self.contentViewaddSubview:self.nameLabel];
self.genderLabel = [[UILabelalloc]initWithFrame:CGRectMake(CGRectGetMinX(self.nameLabel.frame),CGRectGetMaxY(self.nameLabel.frame) + kGap, kLabelWidth, kLabelHeight)];
self.genderLabel.text =@"性别 :男";
[self.contentViewaddSubview:self.genderLabel];
self.phoneNumberLabel = [[UILabelalloc]initWithFrame:CGRectMake(CGRectGetMinX(self.genderLabel.frame),CGRectGetMaxY(self.genderLabel.frame) + kGap, kLabelWidth, kLabelHeight)];
self.phoneNumberLabel.text =@"电话 : 6666666";
[self.contentViewaddSubview:self.phoneNumberLabel];
self.hobbyLabel = [[UILabelalloc]initWithFrame:CGRectMake(kGap,CGRectGetMaxY(self.imgView.frame) + kGap, kScreenWidth - kGap * 2,kLabelHeight)];
self.hobbyLabel.numberOfLines =0;
self.hobbyLabel.text = @" 格言 :衣带渐宽终不悔";
[self.contentViewaddSubview:self.hobbyLabel];
self.contentView.backgroundColor = [UIColoryellowColor];
}
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selectedanimated:animated];
// Configure the view for the selected state
}
@end