自定义cell

import “AppDelegate.h”

import “RootTableViewController.h”

@interface AppDelegate ()

@end

@implementation AppDelegate

  • (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [self.window setRootViewController:[[UINavigationController alloc] initWithRootViewController:[RootTableViewController new]]];

    return YES;
    }

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 = @”customCellEruseIdentifier”;

static NSString *const girlCellID = @”girlCellEruseIdentifier”;
@implementation RootTableViewController

//重写试图控制器的初始化方法
-(instancetype)initWithStyle:(UITableViewStyle)style {

if (self = [super initWithStyle:style]) {
    //获取plist文件路径
    NSString *path = [[NSBundle mainBundle]pathForResource:@"CLASS151042" ofType:@"plist"];
    //根据文件创建字典
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path ];
    //获取分组数组
    self.group = [[dict allKeys] mutableCopy];
    [self.group sortUsingSelector:@selector(compare:)];//排序
    //遍历字典,封装model数据

    //字典与开辟空间
    self.dataDict = [[NSMutableDictionary alloc] initWithCapacity:26];
    //遍历
    for(NSString *key in dict) {

    //临时数组
        NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:20];
       //遍历每个key对应的数组
        for (NSDictionary *stuDic in dict[key]) {
           //KVC完成赋值
            Student *student = [[Student alloc]initWithDictionary:stuDic];
            //储存在临时数组中
            [tempArray addObject:student];
        }
        //数据加入新的字典容器
        [self.dataDict setObject:tempArray forKey:key];

    }


}
return self;

}

  • (void)viewDidLoad {
    [super viewDidLoad];

// NSString *string = @”我的爱好是打篮球。几乎每天都打,这不我又出现在篮球场上了,你知道我怎么喜欢篮球的吗?告诉你吧,以前我也没打过篮球。可我喜欢看“NBA和CBA”的比赛。那些球员非常了得,抢篮板可帅了,手一勾球便到手了,那三分球更是不在话下,他们的每一个动作都是那样的潇洒自如。我心里也渴望和他们一样当个球星。我的偶像是姚明和乔丹。瞧,我的床头还贴着他俩的照片呢!爸爸说:“既然你的偶像都是篮球明星,你也就学打篮球吧,同时也学习他们在球场上的那种拼搏精神。”于是我每天都坚持打一会篮球,渐渐地便爱上了篮球。打了几个月,我发现自己长壮了,饭量也大了许多。在学习上更爱动脑筋了,所以我更喜欢打篮球了。”;
//根据文本计算出高度
//第一个参数限定了在375宽度下计算出此高度,并且高度最多是1000
//第三个参数先顶了在12号字体下计算出来的高度
// CGRect rect = [string boundingRectWithSize:CGSizeMake(375, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil];
//
// NSLog(@”%lf”,rect.size.height);

//注册cell
[self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:customCellID];


[self.tableView registerClass:[GirlCell class] 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 {
    [super didReceiveMemoryWarning];
    // 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.group objectAtIndex:section];
    //根据当前的分组名获取对应的分组数组
    NSArray *array = [self.dataDict objectForKey:key];
    //返回当前分组的元素个数
    return array.count;

// return [self.dataDict[self.group[section] ] count];
}

  • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //找到当前分组名
    NSString *key = [self.group objectAtIndex:indexPath.section];
    //根据分组名找到对应的分组数据
    NSArray *array = [self.dataDict objectForKey: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 = [tableView dequeueReusableCellWithIdentifier:customCellID forIndexPath:indexPath];

    //cell赋值
    cell.student = student;

    CGRect labelFrame = cell.hobbyLabel.frame;
    labelFrame.size.height = height;
    cell.hobbyLabel.frame = labelFrame;

    //不一定要先对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 = [UIColor colorWithRed:red green:green blue:blue alpha:0.5];

    return cell;
    }
    //重新调整高度
    -(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath {

    return 1000;
    }

define kGap (kScreenWidth/18)

define kScreenWidth [UIScreen mainScreen].bounds.size.width

//计算字符串所占高度的方法
-(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;

}

import

import “Student.h”

@interface CustomCell : UITableViewCell

@property(strong,nonatomic) UIImageView *imgView;
@property(strong,nonatomic) UILabel *nameLabel;
@property(strong,nonatomic) UILabel *genderLabel;
@property(strong,nonatomic) UILabel *phoneNumberLabel;
@property(strong,nonatomic) UILabel *QQNumberLabel;
@property(strong,nonatomic) UILabel *hobbyLabel;

@property(strong,nonatomic) Student *student;

@end

import “CustomCell.h”

define kScreenWidth [UIScreen mainScreen].bounds.size.width

define kScreenHeight [UIScreen mainScreen].bounds.size.height

@implementation CustomCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier ]) {

    [self drawView];



}

return self;

}

//重写model对象的setter,外界在复制的时候就会触发此方法
-(void)setStudent:(Student *)student {

if(_student != student) {

    _student = nil;
    _student = student;

}
//cell赋值
self.imgView.image = [UIImage imageNamed:student.picture];
self.nameLabel.text = [NSString stringWithFormat:@"姓名 : %@", student.name];
self.genderLabel.text = [NSString stringWithFormat:@"性别 : %@", student.gender];
self.phoneNumberLabel.text = student.phoneNumber;
self.hobbyLabel.text = student.hobby;

}

define kGap (kScreenWidth/18)

define kImgWidth (kScreenWidth/3)

define kImgHeight (kScreenWidth/3 * 4 /3)

define kLabelWidth (kScreenWidth /2)

define kLabelHeight ((kImgHeight -3 *kGap) / 4)

-(void)drawView {

self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(kGap, kGap, kImgWidth, kImgHeight)];
self.imgView.image = [UIImage imageNamed:@"6.jpg"];
self.imgView.layer.cornerRadius = 15;
self.imgView.layer.masksToBounds = YES;
[self.contentView addSubview:self.imgView];

self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.imgView.frame) + kGap, kGap, kLabelWidth, kLabelHeight)];
self.nameLabel.backgroundColor = [UIColor yellowColor];
self.nameLabel.layer.cornerRadius = 5;
self.nameLabel.layer.masksToBounds =YES;
self.nameLabel.font = [UIFont systemFontOfSize:22];
self.nameLabel.text = @"姓名: 王军";
[self.contentView addSubview:self.nameLabel];

self.genderLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.nameLabel.frame), CGRectGetMaxY(self.nameLabel.frame)+kGap, kLabelWidth, kLabelHeight)];
self.genderLabel.backgroundColor = [UIColor yellowColor];
self.genderLabel.layer.cornerRadius = 5;
self.genderLabel.layer.masksToBounds =YES;
self.genderLabel.font = [UIFont systemFontOfSize:22];
self.genderLabel.text = @"性别:   男";
[self.contentView addSubview:self.genderLabel];

self.phoneNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.genderLabel.frame), CGRectGetMaxY(self.genderLabel.frame)+kGap, kLabelWidth, kLabelHeight)];
self.phoneNumberLabel.backgroundColor = [UIColor yellowColor];
self.phoneNumberLabel.layer.cornerRadius = 5;
self.phoneNumberLabel.layer.masksToBounds =YES;
self.phoneNumberLabel.font = [UIFont systemFontOfSize:22];
self.phoneNumberLabel.text = @"电话 : 66666";
[self.contentView addSubview:self.phoneNumberLabel];

self.QQNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.phoneNumberLabel.frame), CGRectGetMaxY(self.phoneNumberLabel.frame)+kGap, kLabelWidth, kLabelHeight)];
self.QQNumberLabel.backgroundColor = [UIColor yellowColor];
self.QQNumberLabel.layer.cornerRadius = 5;
self.QQNumberLabel.layer.masksToBounds =YES;
self.QQNumberLabel.font = [UIFont systemFontOfSize:22];
self.QQNumberLabel.text = @"QQ : 1234567";
[self.contentView addSubview:self.QQNumberLabel];

self.hobbyLabel = [[UILabel alloc] initWithFrame:CGRectMake(kGap, CGRectGetMaxY(self.imgView.frame)+kGap, kScreenWidth - kGap * 2, kLabelHeight)];
self.hobbyLabel.backgroundColor = [UIColor greenColor];
self.hobbyLabel.layer.cornerRadius = 5;
self.hobbyLabel.layer.masksToBounds =YES;
self.hobbyLabel.font = [UIFont systemFontOfSize:22];
self.hobbyLabel.text = @"曾经沧海难为水,哈哈哈";
self.hobbyLabel.numberOfLines = 0;
[self.contentView addSubview:self.hobbyLabel];

self.contentView.backgroundColor = [UIColor cyanColor];

}

import

import “Student.h”

@implementation Student
//重写无法正确赋值的key-Value对,保证KVC对model对象赋值不出错
-(void)setValue:(id)value forUndefinedKey:(nonnull NSString *)key {

NSLog(@"==%@",key);

}
//打印方法,辅助校验model对象的属性,查看是否成功
-(NSString *)description {
return [NSString stringWithFormat:@”%@,%@,%@,%@,%@”,_name,_age,_gender,_hobby,_picture];

}

//自定义初始化方法
-(instancetype)initWithDictionary:(NSDictionary *)dict{

if (self = [super init]) {
    //setValuesForKeys是修改值
    [self setValuesForKeysWithDictionary:dict];
}


return self;

}

@end

import

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

import “GirlCell.h”

define kScreenWidth [UIScreen mainScreen].bounds.size.width

define kScreenHeight [UIScreen mainScreen].bounds.size.height

@implementation GirlCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

    [self drawView];
}
return self;

}
//重写model对象的setter,外界在复制的时候就会触发此方法
-(void)setStudent:(Student *)student {

if(_student != student) {

    _student = nil;
    _student = student;

}
//cell赋值
self.backImgView.image = [UIImage imageNamed:student.picture];
self.imgView.image = [UIImage imageNamed:student.picture];
self.nameLabel.text = [NSString stringWithFormat:@"姓名 : %@", student.name];
self.genderLabel.text = [NSString stringWithFormat:@"性别 : %@", student.gender];
self.phoneNumberLabel.text = student.phoneNumber;
self.hobbyLabel.text = student.hobby;

}

define kGap (kScreenWidth/18)

-(void)drawView {

self.backImgView = [[UIImageView alloc] initWithFrame:CGRectMake(kGap, kGap, kScreenWidth-2*kGap, 320-2*kGap)];
self.backImgView.layer.cornerRadius = 20;
self.backImgView.layer.masksToBounds =YES;


self.backImgView.image = [UIImage imageNamed:@"6.jpg"];
[self.contentView addSubview:self.backImgView];
//模糊效果视图
UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *visualEffect = [[UIVisualEffectView alloc] initWithFrame:self.backImgView.bounds];
visualEffect.effect = blur;
[self.backImgView addSubview:visualEffect];

define kNameWidth self.backImgView.bounds.size.width / 3

define kNameHeight self.backImgView.bounds.size.height / 5

self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(kGap, kGap, kNameWidth, kNameHeight)];
self.nameLabel.text = @"网聚";
[self.backImgView addSubview:self.nameLabel];

define kGenderWidth (self.backImgView.bounds.size.width / 2 - 3*kGap)

self.genderLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.nameLabel.frame)+kGap, kGap, kGenderWidth, kNameHeight)];
self.genderLabel.text = @"性别 :  女";
[self.backImgView addSubview:self.genderLabel];
/*******************------------*************************/

UIView *view = [[UIView alloc]  initWithFrame:CGRectMake(kGap, CGRectGetMaxY(self.nameLabel.frame)+kGap, self.backImgView .bounds.size.width - 2*kGap, 1)];
view.backgroundColor = [UIColor cyanColor];
[self.backImgView addSubview:view];

define kImgWidth kNameWidth

define kImgHeight self.backImgView.bounds.size.height - 4 *kGap - kNameHeight

self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(kGap, CGRectGetMaxY(view.frame)+kGap, kImgWidth, kImgHeight)];
self.imgView.image = [UIImage imageNamed:@"6.jpg"];
[self.backImgView addSubview:self.imgView];

define kHobbyWidth kGenderWidth

define kHobbyHeight kImgHeight

self.hobbyLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.genderLabel.frame), CGRectGetMaxY(self.genderLabel.frame)+2*kGap, kHobbyWidth, kHobbyHeight)];
self.hobbyLabel.text = @"吾至爱汝,即此爱汝一念,使吾勇于就死也";
self.hobbyLabel.numberOfLines = 6;
[self.backImgView addSubview:self.hobbyLabel];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值