iOS自学之路iOS学习笔记iOS开发笔录

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

2016-02-17  本文已影响68人  Z了个L

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

// 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;
}

NSString *ID = @"status";
- (void)viewDidLoad {
    [super viewDidLoad];

    // self-sizing(iOS8 以后)
//    // 告诉tableView所有cell的高度是自动计算的(根据设置的约束来计算)
//    self.tableView.rowHeight = UITableViewAutomaticDimension;
//
//    // 告诉tableView所有cell的估算高度(减少tableView:heightForRowAtIndexPath:..的调用次数)
    self.tableView.estimatedRowHeight = 200;
//    不把底部@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pictureBottomLc;约束除掉,到时候传数据,根据约束计算的子控件的frame不对

}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//     NSLog(@"cellForRowAtIndexPath--%zd",indexPath.row);
    XMGStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

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

XMGStatusCell *cell;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"heightForRowAtIndexPath--%zd",indexPath.row);
    // 创建一个临时的cell(目的:为了传递indexPath这一行对应的数据,布局内部所有的子控件,得到子控件的frame,进而能够计算cell的高度)
    if (cell == nil) {
       cell = [tableView dequeueReusableCellWithIdentifier:ID];
    }

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

    return cell.cellHeight;
}

@end

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

#define XMGTextFont [UIFont systemFontOfSize:14]
#define XMGNameFont [UIFont systemFontOfSize:17]
@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;
/** cell的高度*/
- (CGFloat)cellHeight;

@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;

@end

@implementation XMGStatusCell

- (void)awakeFromNib
{
    // 手动设置label的文字的最大宽度(目的:为了能够计算label的高度,得到最真实的尺寸)
    self.text_Label.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;
}

// 设置数据
- (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.pictureImageView.hidden = NO;

        self.pictureImageView.image = [UIImage imageNamed:status.picture];
    } else {
        self.pictureImageView.hidden = YES;
    }
}

- (CGFloat)cellHeight
{
    // 强制布局(目的:让label根据设置的约束计算自己最真实尺寸)
    [self layoutIfNeeded];

    CGFloat cellHeight = 0;
    if (self.status.picture) { // 有配图
        cellHeight = CGRectGetMaxY(self.pictureImageView.frame) + 10;
    } else {
        cellHeight = CGRectGetMaxY(self.text_Label.frame) + 10;
    }

    return cellHeight;
}
@end


iOS8之前要删除的约束 图片:

约束效果图片:

上一篇下一篇

猜你喜欢

热点阅读