iOS UICollectionView自定义cell

Author Avatar
人不如故 5月 19, 2017
  • 在其它设备中阅读本文章

自定义的UITableCell

//
//  jokeCell.m
//  MyLaugh
//
//  Created by wangyu on 14/05/2017.
//  Copyright © 2017 wangyu. All rights reserved.
//

#import "jokeCell.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

@implementation jokeCell
{
    UILabel *_titleLabel;
    UILabel *_contentLabel;
    UILabel *_updatetimeLabel;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,5,SCREEN_WIDTH,21)];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = [UIColor purpleColor];
    _titleLabel.font = [UIFont italicSystemFontOfSize:17];
    _contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, 21)];
    _contentLabel.backgroundColor = [UIColor clearColor];
    _contentLabel.numberOfLines = 0;
    _updatetimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, 21)];
    _updatetimeLabel.textAlignment = NSTextAlignmentRight;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    return self;
}


- (void)layoutSubviews{
    [super layoutSubviews];
    [self.contentView addSubview:_titleLabel];
    [self.contentView addSubview:_contentLabel];
    [self.contentView addSubview:_updatetimeLabel];
}

-(void) setTitle:(NSString *)title
{
    _title = title;
    _titleLabel.text = title;
}

-(void) setContent:(NSString *)content
{
    _content = content;
    _contentLabel.text = content;
}

-(void)setUpdatetime:(NSString *)updatetime
{
    _updatetime = updatetime;
    _updatetimeLabel.text = updatetime;
}

-(void) setContentLabelHeight:(CGFloat)height
{
    _contentLabel.frame = CGRectMake(0, 30, _contentLabel.frame.size.width, height);
    _updatetimeLabel.frame = CGRectMake(0, 35+height, _updatetimeLabel.frame.size.width, 21);
}

-(CGFloat) getHeight
{
    //计算文字高度
    NSDictionary *attribute = @{NSFontAttributeName: _contentLabel.font};
    CGSize retSize = [_contentLabel.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, MAXFLOAT) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    _contentLabel.frame = CGRectMake(0, 30, _contentLabel.frame.size.width, retSize.height);
    _updatetimeLabel.frame = CGRectMake(0, 35+_contentLabel.frame.size.height, _updatetimeLabel.frame.size.width, 21);
    return 30+_contentLabel.frame.size.height+10+21+5;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
坚持原创技术分享,您的支持将鼓励我继续创作!