您的位置:首页 > 移动开发 > IOS开发

[iOS基础控件 - 6.7.1] 微博展示 代码

2014-12-06 00:19 393 查看




Controller:

//
//  ViewController.m
//  Weibo
//
//  Created by hellovoidworld on 14/12/4.
//  Copyright (c) 2014年 hellovoidworld. All rights reserved.
//

#import "ViewController.h"
#import "Weibo.h"
#import "WeiboCell.h"
#import "WeiboFrame.h"

@interface ViewController ()

/** 微博数组,类型是WeiboFrame,包含了数据和位置尺寸信息 */
@property(nonatomic, strong) NSArray *weibos;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

// 屏蔽状态栏
- (BOOL)prefersStatusBarHidden {
return YES;
}

#pragma mark -  数据源操作
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.weibos.count;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 传入tableView是为了使用cell缓存池
WeiboCell *cell = [WeiboCell cellWithTableView:self.tableView];

// 传入微博的数据和位置尺寸信息
cell.weiboFrame = self.weibos[indexPath.row];

return cell;
}

#pragma mark - 加载数据
// 延迟加载plist文件中的数据为微博数组
- (NSArray *) weibos {
if (nil == _weibos) {
NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"weibo.plist" ofType:nil]];

NSMutableArray *mdictArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
WeiboFrame *weiboFrame = [[WeiboFrame alloc] init];
Weibo *weibo = [Weibo weiboWithDictionary:dict];

// 传入weibo模型数据到frame模型,内部保存数据,计算各个控件的位置、尺寸
weiboFrame.weibo = weibo;

[mdictArray addObject:weiboFrame];
}

_weibos = mdictArray;
}

return _weibos;
}

#pragma mark - 代理操作
// 动态调整每个cell的高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
WeiboFrame *weiboFrame = self.weibos[indexPath.row];
return weiboFrame.cellHeight;
}

@end


View:

//
//  WeiboCell.h
//  Weibo
//
//  Created by hellovoidworld on 14/12/5.
//  Copyright (c) 2014年 hellovoidworld. All rights reserved.
//

// 用于装载每个TabelViewCell的model
#import <UIKit/UIKit.h>

@class WeiboFrame;
@interface WeiboCell : UITableViewCell

// 微博frame,内持有微博数据和尺寸、位置信息
@property(nonatomic, strong) WeiboFrame *weiboFrame;

// 自定义带有父控件tableView初始化方法
+ (instancetype) cellWithTableView:(UITableView *) tableView;

@end


//
//  WeiboCell.m
//  Weibo
//
//  Created by hellovoidworld on 14/12/5.
//  Copyright (c) 2014年 hellovoidworld. All rights reserved.
//

#import "WeiboCell.h"
#import "WeiboFrame.h"
#import "Weibo.h"

// 昵称字体
#define NAME_FONT [UIFont systemFontOfSize:14]
// 博文字体
#define TEXT_FONT [UIFont systemFontOfSize:15]

@interface WeiboCell()

// 创建各个子控件的成员,用来分离数据赋值和尺寸、位置调整
/** 头像 */
@property(nonatomic, weak) UIImageView *iconView;

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

/** vip标志 */
@property(nonatomic, weak) UIImageView *vipView;

/** 博文 */
@property(nonatomic, weak) UILabel *textView;

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

@end

@implementation WeiboCell

- (void)awakeFromNib {
// Initialization code
}

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

// Configure the view for the selected state
}

#pragma mark - 初始化
// 自定义带有父控件tableView初始化方法
+ (instancetype) cellWithTableView:(UITableView *) tableView {
static NSString *ID = @"weibo";

// 从缓存池寻找
WeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

// 使用重写的构造方法初始化
if (nil == cell) {
cell = [[WeiboCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}

return  cell;
}

// 重写缓存池初始化方法,加入各个子控件,可以设置静态数据,但是没有动态的数据和位置尺寸信息
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// 1.头像
/**
由于self.iconView是weak类型,不能写成:
self.iconView = [[UIImageView alloc] init];
会被立即释放,不能正常赋值,下同
*/
UIImageView *iconView = [[UIImageView alloc] init];
[self.contentView addSubview:iconView];
self.iconView = iconView;

// 2.昵称
UILabel *nameView = [[UILabel alloc] init];
// 指定字体用来计算占用的尺寸大小
nameView.font = NAME_FONT;
[self.contentView addSubview:nameView];
self.nameView = nameView;

// 3.vip标志
UIImageView *vipView = [[UIImageView alloc] init];
vipView.image = [UIImage imageNamed:@"vip"];
[self.contentView addSubview:vipView];
self.vipView = vipView;

// 4.博文
UILabel *textView = [[UILabel alloc] init];
textView.font = TEXT_FONT;
textView.numberOfLines = 0;// 设置自动换行
[self.contentView addSubview:textView];
self.textView = textView;

// 5.配图
UIImageView *pictureView = [[UIImageView alloc] init];
[self.contentView addSubview:pictureView];
self.pictureView = pictureView;
}

return self;
}

#pragma mark - 数据加载
// 加载数据的时候设置数据和尺寸、位置
- (void)setWeiboFrame:(WeiboFrame *)weiboFrame {
_weiboFrame = weiboFrame;

// 1.设置数据
[self calWeiboData];

// 2.设置尺寸、位置
[self calWeiboFrame];
}

// 设置数据
- (void) calWeiboData {
Weibo *weibo = self.weiboFrame.weibo;

// 1.头像
self.iconView.image = [UIImage imageNamed:weibo.icon];

// 2.昵称
self.nameView.text = weibo.name;

// 3.vip标志
if (weibo.vip) {
self.vipView.hidden = NO;
}
else {
self.vipView.hidden = YES;
}

// 4.博文
self.textView.text = weibo.text;

// 5.配图
if (weibo.picture) {
self.pictureView.hidden = NO;
self.pictureView.image = [UIImage imageNamed:weibo.picture];
}
else {
self.pictureView.hidden = YES;
self.pictureView.image = nil;
}
}

// 设置位置、尺寸
- (void) calWeiboFrame {
// 1.头像
self.iconView.frame = self.weiboFrame.iconFrame;

// 2.昵称
self.nameView.frame = self.weiboFrame.nameFrame;

// 3.vip标志
self.vipView.frame = self.weiboFrame.vipFrame;

// 4.博文
self.textView.frame = self.weiboFrame.textFrame;

// 5.配图
if (self.weiboFrame.weibo.picture) {
self.pictureView.frame = self.weiboFrame.pictureFrame;
}
}

@end


Model:

//
//  Weibo.h
//  Weibo
//
//  Created by hellovoidworld on 14/12/5.
//  Copyright (c) 2014年 hellovoidworld. All rights reserved.
//

// 装在微博数据的model
#import <Foundation/Foundation.h>

@interface Weibo : NSObject

#pragma mark - 成员变量
/** 头像 */
@property(nonatomic, copy) NSString *icon;

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

/** vip标志 */
@property(nonatomic, assign) BOOL vip;

/** 博文 */
@property(nonatomic, copy) NSString *text;

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

#pragma mark - 自定义初始化方法
/** 使用字典赋值成员 */
- (instancetype) initWithDictionary:(NSDictionary *) dictionary;

/** 使用字典赋值成员 */
+ (instancetype) weiboWithDictionary:(NSDictionary *) dictionary;

/** 返回空的model */
+ (instancetype) weibo;

@end


//
//  Weibo.m
//  Weibo
//
//  Created by hellovoidworld on 14/12/5.
//  Copyright (c) 2014年 hellovoidworld. All rights reserved.
//

#import "Weibo.h"

@implementation Weibo

/** 使用字典赋值成员 */
- (instancetype) initWithDictionary:(NSDictionary *) dictionary {
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dictionary];
}

return self;
}

/** 使用字典赋值成员 */
+ (instancetype) weiboWithDictionary:(NSDictionary *) dictionary {
return [[self alloc] initWithDictionary:dictionary];
}

/** 返回空的model */
+ (instancetype) weibo {
return [self weiboWithDictionary:nil];
}

@end


//
//  WeiboFrame.h
//  Weibo
//
//  Created by hellovoidworld on 14/12/5.
//  Copyright (c) 2014年 hellovoidworld. All rights reserved.
//

// 装在了每个cell的位置、尺寸和微博数据的model

@class Weibo;
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> // CGRect需要引入UIKit

@interface WeiboFrame : NSObject

// 微博数据
@property(nonatomic, strong) Weibo *weibo;

/** 头像 */
@property(nonatomic, assign, readonly) CGRect iconFrame;

/** 昵称 */
@property(nonatomic, assign, readonly) CGRect nameFrame;

/** vip标志 */
@property(nonatomic, assign, readonly) CGRect vipFrame;

/** 博文 */
@property(nonatomic, assign, readonly) CGRect textFrame;

/** 配图 */
@property(nonatomic, assign, readonly) CGRect pictureFrame;

/** 一条微博cell的高度 */
@property(nonatomic, assign, readonly) CGFloat cellHeight;

@end


//
//  WeiboFrame.m
//  Weibo
//
//  Created by hellovoidworld on 14/12/5.
//  Copyright (c) 2014年 hellovoidworld. All rights reserved.
//

#import "WeiboFrame.h"
#import "Weibo.h"

// 昵称字体
#define NAME_FONT [UIFont systemFontOfSize:14]
// 博文字体
#define TEXT_FONT [UIFont systemFontOfSize:15]

@implementation WeiboFrame

#pragma mark - 加载数据
// 加载数据,用以计算各个控件的位置、尺寸
- (void)setWeibo:(Weibo *)weibo {
_weibo = weibo;

// 间隙参数
CGFloat padding = 10;

// 1.头像
CGFloat iconWidth = 30;
CGFloat iconHeight = 30;
CGFloat iconX = padding;
CGFloat iconY = padding;
_iconFrame = CGRectMake(iconX, iconY, iconWidth, iconHeight);

// 2.昵称
// 计算昵称占用的size
CGSize nameSize = [self calTextSizeWithText:self.weibo.name font:TEXT_FONT maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];

CGFloat nameX = CGRectGetMaxX(_iconFrame) + padding;
CGFloat nameY = iconY + (iconHeight - nameSize.height) / 2;// 居中
_nameFrame.size = nameSize;
_nameFrame.origin = CGPointMake(nameX, nameY);

// 3.vip标志
CGFloat vipWith = 14;
CGFloat vipHeight = 14;
CGFloat vipX = CGRectGetMaxX(_nameFrame) + padding;
CGFloat vipY = nameY;
_vipFrame = CGRectMake(vipX, vipY, vipWith, vipHeight);

// 4.博文
CGSize textSize = [self calTextSizeWithText:self.weibo.text font:TEXT_FONT maxSize:CGSizeMake(300, MAXFLOAT)];
CGFloat textX = padding;
CGFloat textY = CGRectGetMaxY(_iconFrame) + padding;
_textFrame = CGRectMake(textX, textY, textSize.width, textSize.height);

// 5.配图
if (self.weibo.picture) {
CGFloat pictureWidth = 100;
CGFloat pictureHeight = 100;
CGFloat pictureX = padding;
CGFloat pictureY = CGRectGetMaxY(_textFrame) + padding;
_pictureFrame = CGRectMake(pictureX, pictureY, pictureWidth, pictureHeight);

_cellHeight = CGRectGetMaxY(_pictureFrame) + padding; //计算cell高度
}
else {
_cellHeight = CGRectGetMaxY(_textFrame) + padding;
}
}

// 使用自带方法计算一段文字占用的size
- (CGSize) calTextSizeWithText:(NSString *) text font:(UIFont *) font maxSize:(CGSize) maxSize {
NSDictionary *attrs = @{NSFontAttributeName : font};

return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

@end


weibo.plist:




images:


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐