iOS开发笔录iOS学习笔记iOS Developer

[自定义不等高的cell]-storyboard方式iOS8之后

2016-02-17  本文已影响219人  Z了个L
// ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UITableViewController


@end

// ViewController.m
#import "ViewController.h"
#import "XMGStatusCell.h"
#import "XMGStatus.h"
#import "MJExtension.h"

@interface ViewController ()
/** 所有的微博模型*/
@property (nonatomic ,strong) NSArray *statuses;
@end

@implementation ViewController


- (NSArray *)statuses
{
    if (!_statuses) {
        _statuses = [XMGStatus mj_objectArrayWithFilename:@"statuses.plist"];
    }
    return _statuses;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    // iOS8之后
    self.tableView.rowHeight = UITableViewAutomaticDimension;

    self.tableView.estimatedRowHeight =  44;

}

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.statuses.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 去缓存池里面找,如果没有找到,去storyboard里面找
    static NSString *ID = @"status";
    XMGStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 传递模型数据
    cell.status = self.statuses[indexPath.row];
    return cell;
}

@end

// 模型数据
// XMGStatus.h
#import <UIKit/UIKit.h>

@interface XMGStatus : NSObject

/** 图像*/
@property (nonatomic ,copy) NSString *icon;

/** 昵称*/
@property (nonatomic ,copy) NSString *name;

/** 内容(正文)*/
@property (nonatomic ,copy) NSString *text;

/** vip*/
@property (nonatomic ,assign ,getter=isVip) BOOL vip;

/** 配图*/
@property (nonatomic ,copy) NSString *picture;

@end


// XMGStatus.m
#import "XMGStatus.h"

@implementation XMGStatus

@end

// XMGStatusCell.h
#import <UIKit/UIKit.h>

@class XMGStatus;
@interface XMGStatusCell : UITableViewCell

/** 微博模型*/
@property (nonatomic ,strong) XMGStatus *status;

@end

// XMGStatusCell.m
#import "XMGStatusCell.h"
#import "XMGStatus.h"


@interface XMGStatusCell ()
/** 图像*/
@property (nonatomic ,weak) IBOutlet UIImageView *iconImageView;

/** 昵称*/
@property (nonatomic ,weak) IBOutlet UILabel *nameLabel;

/** vip*/
@property (nonatomic ,weak) IBOutlet UIImageView *vipImageView;

/** 正文*/
@property (nonatomic ,weak) IBOutlet UILabel *text_Label;

/** 配图*/
@property (nonatomic ,weak) IBOutlet UIImageView *pictureImageView;

/** 配图的高度约束*/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pictureHLc;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pictureBottomLc;


@end

@implementation XMGStatusCell

// 设置数据
- (void)setStatus:(XMGStatus *)status
{
    _status = status;
    self.iconImageView.image = [UIImage imageNamed:status.icon];

    self.nameLabel.text = status.name;

    if (status.isVip) { // 是VIP
        self.vipImageView.hidden = NO;
        self.nameLabel.textColor = [UIColor orangeColor];
    } else {
        self.vipImageView.hidden = YES;
        self.nameLabel.textColor = [UIColor blackColor];
    }

    self.text_Label.text = status.text;

    if (status.picture) { // 有配图
        self.pictureHLc.constant = 100;
        self.pictureBottomLc.constant = 10;
        self.pictureImageView.image = [UIImage imageNamed:status.picture];
    } else {
        self.pictureHLc.constant = 0;
        self.pictureBottomLc.constant = 0;
    }

}
@end

约束图片:

上一篇 下一篇

猜你喜欢

热点阅读