UI第十三天

注意

1.UITableViewCell自定义有多少种格式就自定义多少个customTableViewCell


2.自定义cell定义东西都加到self.contentView

[self.contentView addSubview:_imageV3];


3.MVC:

MVC设计模式

M:model 数据模型

V:view 视图

C:controller 控制器


4.MVC步骤:

(1).创建一个继承于NSObject的子类:CellModel

(2)CellModel中声明的NSString或NSArray的命名最好与plist文件中的名字相同:

@property (copy,nonatomic) NSString *name;

如果不同:

@property (copy,nonatomic) NSString *introStr;


cellModel文件的实现中:

- (void)setValue:(id)value forUndefinedKey:(NSString *)key

{

    //plist文件中的key名为name,cellModel中声明的属性名为nameStr,下面这样做也能识别nameStr,plist文件中的key名与关键字重名是可以这样做

    if ([key isEqualToString:@"name"])

    {

        self.nameStr = value;

    }

}


(3).自定义cell头文件中:

a.@class CellModel;//前置声明 import区别 避免重复

//model当做cell的属性:

@property (strong,nonatomic) CellModel *model;


(4)自定义cell实现文件中:

a.#import "CellModel.h"


b.- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

这个里面只创建头文件中声明的东西:

 _nameLabel = [[UILabel alloc] init];

        _nameLabel.font = [UIFont boldSystemFontOfSize:18];

        [self.contentView addSubview:_nameLabel];


c.重写model的setter方法

- (void)setModel:(CellModel *)model

{

    _model = model;

    //赋值

    _imageV1.image = [UIImage imageNamed:[_model.icon stringByAppendingString:@".jpg"]];

    _titleLabel.text = model.nameStr;

    

    _timeLabel.text = model.time;

    

    _detailLabel.text = model.text;


    //计算位置大小

     _imageV1.frame = CGRectMake(10, 20, 50, 50);

    

     _titleLabel.frame = CGRectMake(CGRectGetMaxX(_imageV1.frame)+5, _imageV1.frame.origin.y, SCREENWH - CGRectGetMaxX(_imageV1.frame), 30);

    

    _timeLabel.frame = CGRectMake(_titleLabel.frame.origin.x, CGRectGetMaxY(_titleLabel.frame), _titleLabel.frame.size.width, _imageV1.frame.size.height-_titleLabel.frame.size.height);

    //根据字的多少来计算frame.size.height

    CGRect frame = [model.text boundingRectWithSize:CGSizeMake(SCREENWH - 20, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil];

    

    _detailLabel.frame = CGRectMake(_imageV1.frame.origin.x, CGRectGetMaxY(_imageV1.frame), SCREENWH - 20, frame.size.height);

    

    for (UIView *view in self.contentView.subviews)

    {

        if ([view isKindOfClass:[UIImageView class]])

        {

            if (view.tag != 100)

            {

                [view removeFromSuperview];

            }

        }

    }

    NSInteger count = model.images.count;

    CGFloat imageW = (SCREENWH - 20)/3;

    for (int i = 0; i < count; ++i)

    {

        NSInteger x = i % 3;

        NSInteger y = i / 3;

        UIImageView *imageVi = [[UIImageView alloc] initWithFrame:CGRectMake(10+imageW*x, CGRectGetMaxY(_detailLabel.frame)+y*imageW, imageW, imageW)];

        imageVi.image = [UIImage imageNamed:[model.images[i] stringByAppendingString:@".jpg"]];

        [self.contentView addSubview:imageVi];

    }

}


(5).controller中

a.

#import "CustomTableViewCell.h"

#import "CellModel.h"


b.

/*

 什么是KVC? key-value-coding 键值对编码

 */

//dataArray懒加载

- (NSMutableArray *)dataArray

{

    if (_dataArray == nil)

    {

        NSString *path = [[NSBundle mainBundle] pathForResource:@"QQ.plist" ofType:@""];

        NSArray *array = [NSArray arrayWithContentsOfFile:path];

        _dataArray = [NSMutableArray array];

        for (NSDictionary *dic in array)

        {

            CellModel *model = [[CellModel alloc] init];

            [model setValuesForKeysWithDictionary:dic];

            //上面这样用必须保证cellModel中的属性名与plist文件一样

            [_dataArray addObject:model];

        }

    }

    return _dataArray;

}


c.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    CellModel *model = self.dataArray[indexPath.row];

    //计算文字的高度

    CGRect frame = [model.text boundingRectWithSize:CGSizeMake(SCREENWH - 20, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil];

    //图片的高度

    NSInteger count = model.images.count;

    CGFloat imageH;

    if (count != 0)

    {

        if (count >= 0 && count < 4)

        {

            imageH = (SCREENWH-20)/3;

        }

        else if (count >= 4 && count < 7)

        {

            imageH = 2*(SCREENWH-20)/3;

        }

        else

        {

            imageH = 3*(SCREENWH-20)/3;

        }

    }

    else

    {

        imageH = 0;

    }

    return frame.size.height + 70 + imageH;

}


d.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *cellID = @"cellID";

CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

if (!cell)

{

cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

}

    cell.model = self.dataArray[indexPath.row];

    return cell;

}


5.scrollView中隐藏水平的滑动条

_scrollV1.showsHorizontalScrollIndicator = NO;


一些概念:

1.#pragma mark ---数据源懒加载 *****

- (NSMutableArray *)dataArray

{

    if (_dataArray == nil)

    {

        NSString *path = [[NSBundle mainBundle]pathForResource:@"CellModel" ofType:@"plist"];

        _dataArray = [NSMutableArray arrayWithContentsOfFile:path];

    }

    return _dataArray;

}


2.#pragma mark ---table的懒加载

- (UITableView *)table

{

    if (_table == nil)

    {

        _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20) style:UITableViewStylePlain];

        _table.delegate = self;

        _table.dataSource = self;

        _table.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

    }

    return _table;

}


3.自定义cell中 重写的是- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值