自定义UITableViewCell (通过代码实现:每个CELL格式不一样)

来源:互联网 发布:开源网络管理软件 编辑:程序博客网 时间:2024/05/23 19:16

1,删除自动storyboard自动产生的UIViewController,而新拖尾哦UITableViewController,并且设置该VIEW属于自定义控制器类,由于UITableViewController已经实现UITableViewDatasource和UITableViewDelegate,且设定了当前控制器类作为UITableView的代理,所以不用再写一次


2,自定义CELL继承自UITableViewCell


3,计算CELL的frame和高度只计算一次


4,两个模型对象:数据模型和数据frame模型,让数据模型作为frame模型的一个属性,控制器中对frame模型进行关联


5,自定义CELL中创建CELL, 并重写自定义CELL的init方法(初始化一次性的控件、属性等)


6,今天看了视频后,较好完成了代码的重写:



#import <Foundation/Foundation.h>


@interface TXStatus : NSObject


@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *text;
@property (nonatomic, copy) NSString *picture;
@property (nonatomic, assign, getter = isVip) BOOL vip;


+ (instancetype)statusWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end

==============

#import "TXStatus.h"


@implementation TXStatus
+ (instancetype)statusWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}


- (instancetype)initWithDict:(NSDictionary *)dict
{
    if(self = [super init])
    {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}
@end

=========

#import <Foundation/Foundation.h>


@class TXStatus;


@interface TXStatusFrame : NSObject


@property (nonatomic, assign, readonly) CGRect iconF;


@property (nonatomic, assign, readonly) CGRect nameF;


@property (nonatomic, assign, readonly) CGRect vipF;


@property (nonatomic, assign, readonly) CGRect textF;


@property (nonatomic, assign, readonly) CGRect pictureF;


@property (nonatomic, assign, readonly) CGFloat cellHeight;


@property (nonatomic, strong) TXStatus *status;


@end

=========

#define TXNAMEFONT [UIFont systemFontOfSize:15]


#define TXTEXTFONT [UIFont systemFontOfSize:14]


#import "TXStatusFrame.h"
#import "TXStatus.h"


@implementation TXStatusFrame


- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName:font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}


- (void)setStatus:(TXStatus *)status
{
    _status = status;
    
    CGFloat padding = 10;
    //1,iconF
    CGFloat iconW = 30;
    CGFloat iconH = 30;
    CGFloat iconX = padding;
    CGFloat iconY = padding;
    _iconF = CGRectMake(iconX, iconY, iconW, iconH);
    
    //2,nameF
    CGSize nameSize = [self sizeWithText:status.name font:TXNAMEFONT maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    
    CGFloat nameW = nameSize.width;
    CGFloat nameH = nameSize.height;
    CGFloat nameX = CGRectGetMaxX(_iconF) + padding;;
    CGFloat nameY = iconY  + (iconH - nameSize.height) * 0.5;
    _nameF = CGRectMake(nameX, nameY, nameW, nameH);
    
    //3,vipF
    CGFloat vipW = 14;
    CGFloat vipH = 14;
    CGFloat vipX = CGRectGetMaxX(_nameF) + padding;
    CGFloat vipY = nameY;
    _vipF = CGRectMake(vipX, vipY, vipW, vipH);
    
    //4,textF
    CGSize textSize = [self sizeWithText:status.text font:TXTEXTFONT maxSize:CGSizeMake(300, MAXFLOAT)];
    //NSLog(@"%@", NSStringFromCGSize(textSize));
    CGFloat textW = textSize.width;
    CGFloat textH = textSize.height;
    CGFloat textX = padding;
    CGFloat textY = CGRectGetMaxY(_iconF) + padding;
    _textF = CGRectMake(textX, textY, textW, textH);
    
    
    //5,pictureF
    if (status.picture) {
        CGFloat pictureW = 100;
        CGFloat pictureH = 100;
        CGFloat pictureX = padding;
        CGFloat pictureY = CGRectGetMaxY(_textF) + padding;
        _pictureF = CGRectMake(pictureX, pictureY, pictureW, pictureH);
        
        _cellHeight = CGRectGetMaxY(_pictureF) + padding;
    } else {
        _cellHeight = CGRectGetMaxY(_textF) + padding;
    }
}


@end

==================

#import <UIKit/UIKit.h>
@class TXStatusFrame;


@interface TXStatusCell : UITableViewCell


@property (nonatomic, strong) TXStatusFrame *statusFrame;


+ (instancetype)cellWithTableView:(UITableView *)tableView;
@end

==================

#define TXNAMEFONT [UIFont systemFontOfSize:15]


#define TXTEXTFONT [UIFont systemFontOfSize:14]


#import "TXStatusCell.h"
#import "TXStatus.h"
#import "TXStatusFrame.h"


@interface TXStatusCell()


@property (nonatomic, weak)  UIImageView *iconView;
@property (nonatomic, weak)  UILabel *nameView;
@property (nonatomic, weak)  UIImageView *vipView;
@property (nonatomic, weak)  UILabel *textView;
@property (nonatomic, weak)  UIImageView *pictureView;


@end


@implementation TXStatusCell


+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"status";
    TXStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil)
    {
        cell = [[TXStatusCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        UIImageView *icon = [[UIImageView alloc] init];
        [self.contentView addSubview:icon];
        self.iconView = icon;
        
        UILabel *name = [[UILabel alloc] init];
        name.font = TXNAMEFONT;
        [self.contentView addSubview:name];
        self.nameView = name;
        
        UIImageView *vip = [[UIImageView alloc] init];
        vip.image = [UIImage imageNamed:@"vip"];
        [self.contentView addSubview:vip];
        self.vipView = vip;
        
        UILabel *text = [[UILabel alloc] init];
        text.numberOfLines = 0;
        text.font = TXTEXTFONT;
        [self.contentView addSubview:text];
        self.textView = text;
        
        UIImageView *pic = [[UIImageView alloc] init];
        [self.contentView addSubview:pic];
        self.pictureView = pic;
        
    }
    return self;
}


- (void)awakeFromNib
{
    // Initialization code
}


- (void)setStatusFrame:(TXStatusFrame *)statusFrame
{
    _statusFrame = statusFrame;
    
    [self setThisData];
    
    [self setThisFrame];
}


- (void)setThisData
{
    TXStatus *status = self.statusFrame.status;
    self.iconView.image = [UIImage imageNamed:status.icon];
    
    self.nameView.text = status.name;
    
    //self.vipView.image = [UIImage imageNamed:status.vip];
    if(status.isVip){
        self.vipView.hidden = NO;
        self.nameView.textColor = [UIColor redColor];
    } else {
        self.vipView.hidden = YES;
        self.nameView.textColor = [UIColor blackColor];
    }
    
    self.textView.text = status.text;
    
    if(status.picture){
        self.pictureView.image = [UIImage imageNamed:status.picture];
        self.pictureView.hidden = NO;
    }else{
        self.pictureView.hidden = YES;
    }
}


- (void)setThisFrame
{
    TXStatusFrame *statusFrame = self.statusFrame;
    
    self.iconView.frame = statusFrame.iconF;
    self.nameView.frame = statusFrame.nameF;
    self.vipView.frame = statusFrame.vipF;
    self.textView.frame = statusFrame.textF;
    if (self.statusFrame.status.picture) {
        self.pictureView.frame = statusFrame.pictureF;
    }
}




@end

================

#import <UIKit/UIKit.h>


@interface TXViewController : UITableViewController


@end

================

#import "TXViewController.h"
#import "TXStatus.h"
#import "TXStatusFrame.h"
#import "TXStatusCell.h"


@interface TXViewController ()


@property (nonatomic, strong) NSArray *frames;


@end


@implementation TXViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    
    //NSLog(@"%@",[self.frames description]);
}


- (NSArray *)frames
{
    if(_frames == nil)
    {
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil]];
        
        NSMutableArray *framesArray = [NSMutableArray array];
        
        for (NSDictionary *dict in dictArray) {
            TXStatus *status = [TXStatus statusWithDict:dict];
            TXStatusFrame *frame = [[TXStatusFrame alloc] init];
            frame.status = status;
            
            [framesArray addObject:frame];
        }
        _frames = framesArray;
        
    }
    return _frames;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.frames.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TXStatusCell *cell = [TXStatusCell cellWithTableView:tableView];
    
    cell.statusFrame = self.frames[indexPath.row];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TXStatusFrame *statusFrame = self.frames[indexPath.row];
    return statusFrame.cellHeight;
}


- (BOOL)prefersStatusBarHidden
{
    return YES;
}




@end




0 0