IOS开发基础之微博项目



关键性代码

//
//  NJViewController.m
//  06-预习-微博(通过代码自定义cell)//
#import "NJViewController.h"
#import "NJWeiboCell.h"
#import "NJWeibo.h"
#import "NJWeiboFrame.h"@interface NJViewController ()
@property (nonatomic, strong) NSMutableArray *weiboFrames;
@end@implementation NJViewController- (NSMutableArray *)weiboFrames
{if (_weiboFrames == nil) {// 1.加载数据NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"weibo.plist" ofType:nil]];// 2.字典 转成 模型_weiboFrames = [NSMutableArray array];for (NSDictionary *dict in array) {// 创建frame对象NJWeiboFrame *weiboF = [[NJWeiboFrame alloc] init];weiboF.weibo = [NJWeibo weiboWithDict:dict];[_weiboFrames addObject:weiboF];}}return _weiboFrames;
}- (BOOL)prefersStatusBarHidden
{return YES;
}#pragma mark - 数据源方法
#pragma mark 一共有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{return self.weiboFrames.count;
}#pragma mark 每一行显示怎样的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{// 1.去缓存池中取出cellstatic NSString *ID = @"weibo";NJWeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];// 2.缓存池没有cell,重新创建cellif (cell == nil) {cell = [[NJWeiboCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];}// 3.传递模型数据cell.weiboFrame = self.weiboFrames[indexPath.row];return cell;
}#pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{return [self.weiboFrames[indexPath.row] cellHeight];
}@end
//
//  NJWeiboCell.h
//  06-预习-微博(通过代码自定义cell)
////#import <UIKit/UIKit.h>
@class NJWeiboFrame;@interface NJWeiboCell : UITableViewCell
@property (nonatomic, strong) NJWeiboFrame *weiboFrame;
@end
//
//  NJWeiboCell.m
//  06-预习-微博(通过代码自定义cell)
////#import "NJWeiboCell.h"
#import "NJWeibo.h"
#import "NJWeiboFrame.h"@interface NJWeiboCell()
// 1.头像
@property (nonatomic, weak) UIImageView *iconView;
// 2.昵称
@property (nonatomic, weak) UILabel *nameLabel;
// 3.会员图标
@property (nonatomic, weak) UIImageView *vipView;
// 4.正文
@property (nonatomic, weak) UILabel *textView;
// 5.配图
@property (nonatomic, weak) UIImageView *pictureView;
@end@implementation NJWeiboCell- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];if (self) {// 添加内部的子控件// 1.头像UIImageView *iconView = [[UIImageView alloc] init];[self.contentView addSubview:iconView];self.iconView = iconView;// 2.昵称UILabel *nameLabel = [[UILabel alloc] init];nameLabel.font = kNameFont;[self.contentView addSubview:nameLabel];self.nameLabel = nameLabel;// 3.会员图标UIImageView *vipView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"vip.png"]];[self.contentView addSubview:vipView];self.vipView = vipView;// 4.微博正文UILabel *textView = [[UILabel alloc] init];textView.font = kTextFont;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;
}- (void)setWeiboFrame:(NJWeiboFrame *)weiboFrame
{_weiboFrame = weiboFrame;// 1.设置微博数据[self settingData];// 2.设置子控件的frame(x、y、width、height)[self settingSubviewFrame];
}#pragma mark 设置子控件的frame
- (void)settingSubviewFrame
{// 1.头像self.iconView.frame = self.weiboFrame.iconF;// 2.昵称self.nameLabel.frame = self.weiboFrame.nameF;// 3.vipself.vipView.frame = self.weiboFrame.vipF;// 4.正文self.textView.frame = self.weiboFrame.textF;// 5.配图if (self.weiboFrame.weibo.picture) { // 有配图self.pictureView.frame = self.weiboFrame.pictureF;}
}#pragma mark 设置微博数据
- (void)settingData
{NJWeibo *weibo = self.weiboFrame.weibo;// 1.头像self.iconView.image = [UIImage imageNamed:weibo.icon];// 2.昵称self.nameLabel.text = weibo.name;if (weibo.vip) {self.nameLabel.textColor = [UIColor redColor];} else {self.nameLabel.textColor = [UIColor blackColor];}// 3.会员图标self.vipView.hidden = !weibo.vip;// 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;}
}
@end
//
//  NJWeibo.h
//  06-预习-微博(通过代码自定义cell)
////#import <Foundation/Foundation.h>@interface NJWeibo : NSObject
@property (nonatomic, copy) NSString *text; // 内容
@property (nonatomic, copy) NSString *icon; // 头像
@property (nonatomic, copy) NSString *name; // 昵称
@property (nonatomic, copy) NSString *picture; // 配图
@property (nonatomic, assign) BOOL vip;- (id)initWithDict:(NSDictionary *)dict;
+ (id)weiboWithDict:(NSDictionary *)dict;
@end
//
//  NJWeibo.m
//  06-预习-微博(通过代码自定义cell)
//
//  Created by 李南江 on 14-4-21.
//  Copyright (c) 2014年 itcast. All rights reserved.
//#import "NJWeibo.h"@implementation NJWeibo- (id)initWithDict:(NSDictionary *)dict
{if (self = [super init]) {self.name = dict[@"name"];self.vip = [dict[@"vip"] boolValue];self.picture = dict[@"picture"];self.icon = dict[@"icon"];self.text = dict[@"text"];}return self;
}+ (id)weiboWithDict:(NSDictionary *)dict
{return [[self alloc] initWithDict:dict];
}@end
//
//  NJWeiboFrame.h
//  06-预习-微博(通过代码自定义cell)
////#define kNameFont [UIFont systemFontOfSize:15]
#define kTextFont [UIFont systemFontOfSize:15]#import <Foundation/Foundation.h>
@class NJWeibo;@interface NJWeiboFrame : NSObject@property (nonatomic, assign, readonly) CGRect iconF;
@property (nonatomic, assign, readonly) CGRect nameF;
@property (nonatomic, assign, readonly) CGRect vipF;
@property (nonatomic, assign, readonly) CGRect textF;
@property (nonatomic, assign, readonly) CGRect pictureF;@property (nonatomic, assign, readonly) CGFloat cellHeight;@property (nonatomic, strong) NJWeibo *weibo;@end
//
//  NJWeiboFrame.m
//  06-预习-微博(通过代码自定义cell)
//
//  Created by 李南江 on 14-4-21.
//  Copyright (c) 2014年 itcast. All rights reserved.
//// cell的边框宽度
#define kCellBorder 10
// 头像的宽高
#define kIconWH 30
// vip的宽高
#define kVipWH 14
// 图片尺寸
#define kImageWH 70#import "NJWeiboFrame.h"
#import "NJWeibo.h"@implementation NJWeiboFrame
- (void)setWeibo:(NJWeibo *)weibo
{_weibo = weibo;// 1.头像CGFloat iconX = kCellBorder;CGFloat iconY = kCellBorder;_iconF = CGRectMake(iconX, iconY, kIconWH, kIconWH);// 2.昵称// 计算用户名称的尺寸CGSize nameSize = [_weibo.name sizeWithFont:kNameFont];CGFloat nameX = CGRectGetMaxX(_iconF) + kCellBorder;CGFloat nameY = iconY + (kIconWH - nameSize.height) * 0.5;_nameF = CGRectMake(nameX, nameY, nameSize.width, nameSize.height);// 3.vipCGFloat vipX = CGRectGetMaxX(_nameF) + kCellBorder;CGFloat vipY = nameY;_vipF = CGRectMake(vipX, vipY, kVipWH, kVipWH);// 4.正文CGFloat textX = iconX;CGFloat textY = CGRectGetMaxY(_iconF) + kCellBorder;CGFloat textW = 320 - 2 * kCellBorder;// 计算文字尺寸(显示文字的宽度)CGSize textSize = [_weibo.text sizeWithFont:kTextFont constrainedToSize:CGSizeMake(textW, MAXFLOAT)];_textF = CGRectMake(textX, textY, textW, textSize.height);// 5.配图\计算cell的高度if (_weibo.picture) { // 有配图CGFloat pictureX = textX;CGFloat pictureY = CGRectGetMaxY(_textF) + kCellBorder;_pictureF = CGRectMake(pictureX, pictureY, kImageWH, kImageWH);_cellHeight = CGRectGetMaxY(_pictureF) + kCellBorder;} else { // 没有配图_cellHeight = CGRectGetMaxY(_textF) + kCellBorder;}
}
@end

IOS开发基础之微博项目相关推荐

  1. IOS开发基础之微博项目第1天-OC版

    IOS开发基础之微博项目第1天-OC版 纯代码创建的项目,具有参考价值 该资料来自2014年7月3号,虽然时间过去较长,但是oc和swift不同,oc语法迭代更新慢 具有一定的参考意义 涉及xib加载 ...

  2. IOS开发基础之模拟科技头条项目案例32

    IOS开发基础之模拟科技头条项目案例32 说说这个项目的技术要点核心 ,首先是异步网络请求,block的回调,sdWebImage的使用.各个控件的使用,NSDate日期的转换.自动适配屏幕工作,模型 ...

  3. IOS开发基础之手势解锁项目案例

    IOS开发基础之手势解锁项目案例 项目最终实现效果. 由于缺少红色的error背景图.我自己从安卓项目找到一个手势解锁,然后通过ps添加粉红色的红圈,才得以解决.为了分享给大家源码,github和本地 ...

  4. IOS开发基础之汽车品牌项目-14

    IOS开发基础之汽车品牌项目-14 // // ViewController.m // 16-汽车品牌展示02 // // Created by 鲁军 on 2021/2/3. //#import & ...

  5. IOS开发基础之UI的喜马拉雅的项目-10

    IOS开发基础之UI的喜马拉雅的项目-10 // // ViewController.m // 10-喜马拉雅 // // Created by 鲁军 on 2021/2/2. //#import & ...

  6. iOS开发基础知识--碎片44

    iOS开发基础知识--碎片44  iOS开发基础知识--碎片44 1:App跳转至系统Settings 跳转在IOS8以上跟以下是有区别的,如果是IOS8以上可以如下设置: NSURL *url = ...

  7. IOS开发基础之OC的Block入门_Day09-Block

    IOS开发基础之OC的Block入门_Day09-Block block是oc的重要的基础知识,重点之重.跟协议一样重要,是进行函数回调重要手段.在后续的UI学习具有举足轻重的地位.学会基础的bloc ...

  8. IOS开发基础之音频工具类封装AVAudioPlayer

    IOS开发基础之音频工具类封装AVAudioPlayer 源码在我的主页下面 ,项目名称是AVAudioPlayer 关键性代码 工具类的封装 // // LJAudioTool.h // AVAud ...

  9. IOS开发基础之NSURLSession的使用

    IOS开发基础之NSURLSession的使用 服务器我们选用的是tomcat服务器. 所有项目info.plist加入 <key>NSAppTransportSecurity</k ...

最新文章

  1. Squid3反向代理安装与配置
  2. textarea 自适应窗口|IE、firefox 显示同样的效果
  3. POJ 1423 Big Number
  4. 数据中心机房建设几个重要的国家标准
  5. wireshark找不到接口_wireshark网络小故障分析定位
  6. pop3通过时间或者条件取邮件_Python 进阶(三):邮件的发送与收取
  7. asterisk php agi,asterisk AMI入门
  8. 在控制台环境下调用MFC DLL崩溃的问题小结
  9. C语言调用函数将is替换为be,C语言程序设计课件ppt.ppt
  10. VBS脚本常用经典代码
  11. 【C语言】通讯录制作
  12. [技巧]深入了解强大的 ES6 「 ... 」 运算符
  13. Android MediaProjection 代码分析
  14. goldendict无法导入字典
  15. 【洛谷】地球人口承载力估计【C语言程序】
  16. java计算机毕业设计网课系统源码+系统+数据库+lw文档+mybatis+运行部署
  17. Java 8 forEach使用
  18. 为何沃比帕克模式无法复制?
  19. CentOS6.5 安装wine
  20. JAVASCRIPT中THIS指的是什么?

热门文章

  1. 一维有限元法matlab,一维有限元法解常微分方程
  2. python贪吃蛇设计目标_基于 pygame 设计贪吃蛇游戏
  3. mysql workbench 6.2_如何在MySQL Workbench 6.2查询中运行.sql文件?
  4. 设计和实时视图不一样_新宝骏“星际几何”设计理念,演绎不一样的跨界融合...
  5. java负数转换二进制表示_Java中的负数的在计算机中的二进制表示,以及与十进制的相互转换...
  6. oracle0数据库论文总结,大学oracle数据库总结(考试必备)
  7. hash redis springboot_Redis常见的工作场景使用实战,Redisson分布式锁的实现
  8. php获取本月工作日,ThinkPHP中获取指定日期后工作日的具体日期方法
  9. QT + OpenCV + MinGW 在windows下配置开发环境
  10. 数据结构 | 实现串(定长顺序存储表示法)