小编实在不想多说什么了,直接上代码吧.我觉得我的主要注释写的已经很清楚了.
在此说明小编这里用的是xib的写法,如果在其中的运行中有什么问题,请放心评论,我会在第一时间为大家解答.谢谢支持
代码案例
1.#import <UIKit/UIKit.h>
@interface ZJStatuesTableViewController : UITableViewController
@end
2.#import "ZJStatuesTableViewController.h"
#import "ZJStatuesTableViewCell.h"
#import "ZJStatus.h"
@interface ZJStatuesTableViewController ()
/** 存放数据的数组 */
@property (strong, nonatomic) NSArray *statuses;
@end
@implementation ZJStatuesTableViewController
/**
* 懒加载
*
*/
- (NSArray *)statuses
{
if (_statuses == nil) {
// 加载plist中的字典数组
NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
// 字典数组 -> 模型数组
NSMutableArray *statusArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
ZJStatus *status = [ZJStatus statusWithDict:dict];
[statusArray addObject:status];
}
_statuses = statusArray;
}
return _statuses;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// 返回多少行,通过数组中的个数
return self.statuses.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 封装好的自定义cell
ZJStatuesTableViewCell *cell = [ZJStatuesTableViewCell cellWithTableView:tableView];
// 传递模型
cell.status = self.statuses[indexPath.row];
return cell;
}
#pragma mark - 代理方法
/**
* 返回每一行的高度,但是需要清楚调用顺序,如果不加 estimatedHeightForRowAtIndexPath 方法,那么程序首先执行的就是 heightForRowAtIndexPath,然后才是 numberOfRowsInSection,这样的结果就是, 所得到模型是空的. 应该是模型先创建完了才计算cell高度是多少.
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 获取cell对象
ZJStatus *status = self.statuses[indexPath.row];
return status.cellHeight;
}
/**
* 这是一个估计高度算法,这里我们是为了改变程序的执行顺序而写的
*/
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200; // 估计高度是200
}
@end
3.接下来是模型类 ZJStatus.h
//#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> // 因为uikit框架包含了foundation和CoreGraphics等多个框架
//CGFloat 这个不是在Foundation框架中的,而是CoreGraphics框架,所以我直接使用uikit框架将两者全部包含,就不影响工作了
@interface ZJStatus : NSObject
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *text;
@property (strong, nonatomic) NSString *icon;
@property (strong, nonatomic) NSString *picture;
@property (assign, nonatomic, getter=isVip) BOOL vip;
/** cell 高度 */
@property (assign, nonatomic) CGFloat cellHeight;
+ (instancetype)statusWithDict:(NSDictionary *)dict;
@end
4.ZJStatus.m
#import "ZJStatus.h"
@implementation ZJStatus
+(instancetype)statusWithDict:(NSDictionary *)dict
{
ZJStatus *status = [[self alloc] init];
[status setValuesForKeysWithDictionary:dict];
return status;
}
@end
5. 自定义cell类, ZJStatuesTableViewCell.h
#import <UIKit/UIKit.h>
@class ZJStatus;
@interface ZJStatuesTableViewCell : UITableViewCell
/** 模型 */
@property (strong, nonatomic) ZJStatus *status;
+ (instancetype)cellWithTableView:(UITableView *)tableView;
@end
6.ZJStatuesTableViewCell.m
#import "ZJStatuesTableViewCell.h"
#import "ZJStatus.h"
@interface ZJStatuesTableViewCell ()
@property (weak, nonatomic) IBOutlet UIImageView *iconName;
@property (weak, nonatomic) IBOutlet UILabel *nameLable;
@property (weak, nonatomic) IBOutlet UIImageView *vip;
@property (weak, nonatomic) IBOutlet UILabel *contentLable;
@property (weak, nonatomic) IBOutlet UIImageView *pictureView;
@end
@implementation ZJStatuesTableViewCell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString *ID = @"cell";
// 注册cell类,这里小编在以前的几篇博客中写的很清楚了,这是其中创建cell的一种方法.
ZJStatuesTableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
// xib 的特有写法
cell = [[ [ NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}
return cell;
}
/**
* xib的时候程序一进来就会执行这个方法
*/
- (void)awakeFromNib
{
/**
* preferredMaxLayoutWidth
* 设置label每一行的最大宽度
* 为了保证计算出来的数值 跟 真正显示出来的效果一致
*/
self.contentLable.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 40;
}
- (void)setStatus:(ZJStatus *)status
{
_status = status;
// 判断是否是vip
if (status.isVip) {
// 如果是修改名字颜色
self.nameLable.textColor = [UIColor orangeColor];
// 不隐藏vip头像
self.vip.hidden = NO;
}
else
{
// 反之,如果不是,名字颜色为黑色
self.nameLable.textColor = [UIColor blackColor];
// 隐藏vip头像
self.vip.hidden = YES;
}
self.nameLable.text = status.name;
self. iconName.image = [UIImage imageNamed:status.icon];
// 判断是否有配图
if (status.picture) {
// 如果有,配图不需要隐藏
self.pictureView.hidden = NO;
self.pictureView.image = [UIImage imageNamed:status.picture];
}
else
{
// 没有配图,将视图隐藏
self.pictureView.hidden = YES;
}
// 赋值
self.contentLable.text = status.text;
// 强制布局
[self layoutIfNeeded];
// 计算cell的高度
if (self.pictureView.hidden) { // 没有配图
status.cellHeight = CGRectGetMaxY(self.contentLable.frame) + 10;
}
else
{
// 有配图
status.cellHeight = CGRectGetMaxY(self.pictureView.frame) + 10;
}
}
最后xib的布局请大家模仿新浪微博的布局进行布局.如图:
![微博图片](http://img.blog.csdn.net/20160424232947141)
@end