UITableView(微博界面的简单搭建)

通过MVC的设计模式,设计微博界面

1.M--Model

1)数据Model

1>声明

#import <Foundation/Foundation.h>

@interface CZStatus : NSObject

// 用户头像

@property (nonatomic, copy) NSString *icon;

// 用户名

@property (nonatomic, copy) NSString *name;

// 会员

@property (nonatomic, assign) BOOL isVip;

// 微博内容

@property (nonatomic, copy) NSString *text;

// 配图

@property (nonatomic, copy) NSString *picture;

+ (instancetype)statusWithDict:(NSDictionary *)dict;

- (instancetype)initWithDict:(NSDictionary *)dict;

@end

2>实现

#import "CZStatus.h"

@implementation CZStatus

+ (instancetype)statusWithDict:(NSDictionary *)dict {

return [[self alloc] initWithDict:dict];

}

- (instancetype)initWithDict:(NSDictionary *)dict

{

self = [super init];

if (self) {

[self setValuesForKeysWithDictionary:dict];

}

return self;

}

@end

2)每一行cell的frame的model

1>声明

#import <UIKit/UIKit.h>

@class CZStatus;

/// 用户名字体大小

#define nameLabelFont  [UIFont systemFontOfSize:14.0]

/// 微博文字字体大小

#define textLabelFont  [UIFont systemFontOfSize:15.0]

@interface CZStatusFrame : NSObject

// 数据模型

@property (nonatomic, strong) CZStatus *status;

/**

*  头像frame

*/

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

/**

*  昵称frame

*/

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

/**

*  vip图标frame

*/

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

/**

*  正文frame

*/

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

/**

*  配图的frame

*/

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

/**

*  cell的行高

*/

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

@end

2>实现

#import "CZStatusFrame.h"

#import "CZStatus.h"

#define Spacing 8

@implementation CZStatusFrame

// 重写数据模型的set方法,在此方法计算子控件的frame和行高

- (void)setStatus:(CZStatus *)status {

#warning mark - 重写set方法一定不要忘了给下线线的成员变量赋值

_status = status;

// 头像

CGFloat iconX = Spacing;

CGFloat iconY = Spacing;

CGFloat iconW = 35;

CGFloat iconH = iconW;

_iconF = CGRectMake(iconX, iconY, iconW, iconH);

// 昵称

CGFloat nameLX = CGRectGetMaxX(_iconF) + Spacing;

CGFloat nameLY = iconY + Spacing;

CGSize nameSize = [self sizeWithText:_status.name font:nameLabelFont maxSize: CGSizeMake(MAXFLOAT, MAXFLOAT)];

_nameF = (CGRect){{nameLX, nameLY}, nameSize};

// vip图标

if (_status.isVip) {

CGFloat vipX = CGRectGetMaxX(_nameF) + Spacing;

CGFloat vipY = nameLY + Spacing * 0.3;

CGFloat vipW = 15;

CGFloat vipH = 15;

_vipF = CGRectMake(vipX, vipY, vipW, vipH);

}

// 正文

CGFloat textX = iconX;

CGFloat textY = CGRectGetMaxY(_iconF) + Spacing;

CGFloat textMaxWidth = [UIScreen mainScreen].bounds.size.width - 2 *Spacing;

CGSize textSize = [self sizeWithText:_status.text font:textLabelFont maxSize:CGSizeMake(textMaxWidth, MAXFLOAT)];

_textF = (CGRect){{textX, textY}, textSize};

// 配图

if (_status.picture) {

CGFloat picX = iconX;

CGFloat picY = CGRectGetMaxY(_textF) + Spacing;

CGFloat picW = 100;

CGFloat picH = picW;

_picF = CGRectMake(picX, picY, picW, picH);

}

_cellHeight = MAX(CGRectGetMaxY(_picF), CGRectGetMaxY(_textF)) + Spacing;

}

/**

*  计算文字尺寸

*

*  @param text    需要计算的文字

*  @param font    文字字体

*  @param maxSize 文字矩形最大尺寸

*/

- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize {

NSDictionary *attributes = @{NSFontAttributeName : font};

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

}

@end

2.V--View  即视图

cell视图

1)声明

#import <UIKit/UIKit.h>

@class CZStatusFrame;

@interface CZStatusCell : UITableViewCell

// frame模型

@property (nonatomic, strong) CZStatusFrame *statusFrame;

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

@end

2)实现

#import "CZStatusCell.h"

#import "CZStatus.h"

#import "CZStatusFrame.h"

#define Margins 8

@interface CZStatusCell ()

@property (nonatomic, strong) CZStatus *status;

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

/// 快速创建cell

+ (instancetype)cellWithTableView:(UITableView *)tableView {

static NSString *ID = @"cell";

CZStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

if (cell == nil) {

cell = [[CZStatusCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

}

return cell;

}

// 重写initWithStyle:方法,让cell一创建出来就已经有多有子控件

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

if (self) {

// 添加cell的内部子控件

[self addStatusCellWithSubview];

}

return self;

}

// 添加cell的内部子控件

- (void)addStatusCellWithSubview {

// 头像

self.iconView = [self createImageView];

// 昵称

self.nameView = [self createLabelWithFont:nameLabelFont];

// vip图标

self.vipView = [self createImageView];

self.vipView.image = [UIImage imageNamed:@"vip"];

// 正文

self.textView = [self createLabelWithFont:textLabelFont];

// 配图

self.pictureView = [self createImageView];

}

/// 快速创建并添加label

- (UILabel *)createLabelWithFont:(UIFont *)font {

UILabel *label = [[UILabel alloc] init];

label.numberOfLines = 0;

label.font = font;

[self.contentView addSubview:label];

return label;

}

/// 快速创建并添中imageView

- (UIImageView *)createImageView {

UIImageView *imageView = [[UIImageView alloc] init];

[self.contentView addSubview:imageView];

return imageView;

}

/// 重写frame模型的set方法,在此方法设置cell子控件的数据和frame

- (void)setStatusFrame:(CZStatusFrame *)statusFrame {

_statusFrame = statusFrame;

_status = statusFrame.status;

// 1.设置数据

[self settingData];

// 2.计算frame

[self settingFrame];

}

// 设置控件数据

- (void)settingData {

// 设置用户头像

self.iconView.image = [UIImage imageNamed:_status.icon];

// 设置用户名

self.nameView.text = _status.name;

// 设置用户名颜色,会员是红色,普通用户是黑色

self.nameView.textColor =  _status.isVip ? [UIColor redColor] : [UIColor blackColor];

// 是否显示会员

self.vipView.hidden = _status.isVip ? NO : YES;

// 设置微博内容

self.textView.text = _status.text;

// 是否显示配图

self.pictureView.hidden = _status.picture ? NO : YES;

// 设置配图

self.pictureView.image = _status.picture ? [UIImage imageNamed:_status.picture] : nil;

}

/// 设置控件frame

- (void)settingFrame {

// 设置用户头像frame

self.iconView.frame = self.statusFrame.iconF;

// 设置用户名frame

self.nameView.frame = self.statusFrame.nameF;

// 设置vip的frame

self.vipView.frame = _status.isVip ? self.statusFrame.vipF : CGRectZero;

// 设置微博内容的frame

self.textView.frame = self.statusFrame.textF;

// 设置配图的frame

self.pictureView.frame = _status.picture ? self.statusFrame.picF : CGRectZero;

}

@end

3.C--Controller 即控制层

1)声明

#import <UIKit/UIKit.h>

@interface CZViewController : UITableViewController  // 此处继承自UITableViewController

@end

2)实现

#import "CZViewController.h"

#import "CZStatus.h"

#import "CZStatusCell.h"

#import "CZStatusFrame.h"

@interface CZViewController ()

@property (nonatomic, strong) NSArray *statusFrame;

@end

@implementation CZViewController

- (void)viewDidLoad {

[super viewDidLoad];

// 预估行高

self.tableView.estimatedRowHeight = 300;

}

// 隐藏状态栏

- (BOOL)prefersStatusBarHidden {

return YES;

}

#pragma mark - tableView的数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return self.statusFrame.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

// 创建cell

CZStatusCell *cell = [CZStatusCell cellWithTableView:tableView];

// 拿到每行对应的frame模型

CZStatusFrame *statusFrame = self.statusFrame[indexPath.row];

// 给cell的frame模型赋值

cell.statusFrame = statusFrame;

return cell;

}

#pragma mark - 代理方法用来返回行高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

// 返回cell的高度

return [self.statusFrame[indexPath.row] cellHeight];

}

// 懒加载字典转模型

- (NSArray *)statusFrame {

if (_statusFrame == nil) {

NSArray *dictArr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil]];

NSMutableArray *arrM = [NSMutableArray array];

[dictArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

CZStatusFrame *statusFrame = [[CZStatusFrame alloc] init];

CZStatus *status = [CZStatus statusWithDict:obj];

statusFrame.status = status;

[arrM addObject:statusFrame];

}];

_statusFrame = arrM;

}

return _statusFrame;

}

@end

时间: 2024-08-09 00:39:19

UITableView(微博界面的简单搭建)的相关文章

ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局 一.实现效果 二.使用纯代码自定义一个tableview的步骤 1.新建一个继承自UITableViewCell的类 2.重写initWithStyle:reuseIdentifier:方法 添加所有需要显示的子控件(不需要设置子控件的数据和frame,  子控件要添加到contentView中

iOS开发UI基础—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

ios开发UI基础-使用纯代码自定义UItableviewcell实现一个简单的微博界面布局 一.实现效果 二.使用纯代码自定义一个tableview的步骤 1.新建一个继承自UITableViewCell的类 2.重写initWithStyle:reuseIdentifier:方法 添加所有需要显示的子控件(不需要设置子控件的数据和frame,  子控件要添加到contentView中) 进行子控件一次性的属性设置(有些属性只需要设置一次, 比如字体\固定的图片) 3.提供2个模型 数据模型:

(1)Jenkins Linux环境下的简单搭建

(1)Jenkins Linux环境下的简单搭建 Jenkins是一个开源软件项目,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能. ----百度百科 这是一款基于Java开发的工具.种种原因,最近刚开始接触,决定研究一下.Jenkins的搭建方法不止一种,一下就是个人总结的其中一种,文章内容比较浅显,不足之处,欢迎指正. 首先,所需要准备的工具JDK.Maven.资料上显示JDK版本最好高于1.7,并没有研究1.7以下版本,所谓"没有实际调研,就没有发言权",在此就不做过多

solr 简单搭建 数据库数据同步(待续)

原来在别的公司负责过文档检索模块的维护(意思就是不是俺开发的啦).所以就稍微接触和研究了下文档检索. 文档检索其实是全文检索,是通过一种技术把N多文档进行一定规律的切割归类,然后创建易于搜索的索引式文件,然后搜索具有某些规律的文档时,能够通过快速定位索引,然后根据索引提供的信息精确定位到文档从而实现迅速找到文档.这个文档一般成为条目. 上家公司的时候使用的是Lucene加上Zoie实现的.lucene是apache下的开源项目,不过并不是全文检索的实现,而是一个全文检索的引擎,是一个架构,是其他

iOS开发UI篇—以微博界面为例使用纯代码自定义cell程序编码全过程(一)

iOS开发UI篇-以微博界面为例使用纯代码自定义cell程序编码全过程(一) 一.storyboard的处理 直接让控制器继承uitableview controller,然后在storyboard中把继承自uiviewcontroller的控制器干掉,重新拖一个tableview controller,和主控制器进行连线. 项目结构和plist文件 二.程序逻辑业务的处理 第一步,把配图和plist中拿到项目中,加载plist数据(非png的图片放到spooding files中) 第二步,字

iOS开发——开发必备OC篇&amp;UITableView设置界面完整封装(四)

设置界面完整封装(四) 简单MVC实现UITableView设置界面完善封装及拓展使用 关于使用和拓展, 其实基本上就是同UItableView,知识讲数据改一下就可以 拓展使用 1:首先定义一个数组用来装组的模型 // 总共的组数 @property (nonatomic, strong) NSMutableArray *groups; 2:懒数组 1 - (NSMutableArray *)groups 2 3 { 4 5 if (_groups == nil) { 6 7 _groups

Ubuntu学习笔记-win7&Ubuntu双系统简单搭建系统指南

win7&Ubuntu双系统简单搭建系统指南 本文是自己老本子折腾Ubuntu的一些记录,主要是搭建了一个能够足够娱乐(不玩游戏)专注练习自己编程能力的内容.只是简单的写了关于系统的安装和一些配置环境的简单搭建.并没有深入探讨系统地各项内容.希望可以给香简单使用的同学参考. 一.准备工作 打开UltraISO ,依次点击"文件"--"打开"--选择Ubuntu14.04系统镜像文件,确认打开后就能在软件界面内看到整个镜像的全部文件信息. 接下来开始制作系统安

IOS开发学习笔记031-代码实现微博界面

微博界面如下 1.准备资源文件 2.更改父类,实现代理方法 3.新建一个类weiboCell,封装cell的实现,继承自UITableViewCell 4.新建一个类weibo,继承NSObject,封装对数据的操作 5.优化:新建一个WeiboFrame类重新计算每个cell的高度 1.准备资源文件 新建一个plist文件,添加条目,root类型是array,子类型是Dictionary 2.更改父类,实现代理方法 接下来得实现过程如上一篇文章,改变父类为UITableViewControll

简单搭建Gitlab CI持续集成环境

简单搭建Gitlab CI持续集成环境 简单介绍Gitlab CI的功能 从GitLab 8.X 开始,GitLab CI就已经集成在GitLab中,我们只要在项目中添加一个.gitlab-ci.yml文件,然后添加一个Runner,开启Runner,即可进行持续集成.而且随着GitLab的升级,GitLab CI变得越来越强大. GitLab Runner 在没使用过Gitlab之前,我也有一个困惑,到底Gitlab Runner是什么东西.它的作用是什么?</br>GitLab Runne