第四天

  微博数据展示:获取服务器数据,json数据的解析,MVC的使用,自定义cell高度的计算,一些分类的设计。已经是第四天了,虽然每天都有课程,但这个东西也基本完成了一大半吧,一些忘掉的知识也捡起来了。看了一下第一次写的笔记,做到这个程度可是花了将近十多天的时间,这次这么短的时间就做到了着,还是有一点复制粘贴的成分,但是大致的思维过程还是有的,也练了一下编码速度的。四天时间主要就是回顾一下之前做的一些事情,下一步不打算接着做下去了,要将第一次没完成的很多细节完善,争取做得更加完美。简单的记录下今天吧,很多课上的很没意思的说。

  1.很明显需要自定义cell,而微博展示这种比较复杂的cell,我们在一开始就把所有要用到的控件加进去,然后再根据数据选择是否进行显示。

  2.首先是要在home模块中向新浪请求数据,[self loadStatus],方法中我们需要使用账号的access_token作为请求参数,通过SVAccountTool工具类很容易得到。

 - (void)loadStatus
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; NSMutableDictionary *pramas = [NSMutableDictionary dictionary];
// 拿到当前账号
SVAccount *account = [SVAccountTool account];
pramas[@"access_token"] = account.access_token; [manager GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:pramas progress:^(NSProgress * _Nonnull uploadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 将字典数组转为模型数组(里面放的就是IWStatus模型)
NSArray *statusArray = [SVStatus mj_objectArrayWithKeyValuesArray:responseObject[@"statuses"]];
// 创建frame模型对象
NSMutableArray *statusFrameArray = [NSMutableArray array];
for (SVStatus *status in statusArray) {
SVStatusFrame *statusFrame = [[SVStatusFrame alloc] init];
// 传递微博模型数据
statusFrame.status = status;
[statusFrameArray addObject:statusFrame];
}
// 赋值
self.statusFrames = statusFrameArray;
// 刷新表格
[self.tableView reloadData];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"status error--%@", error);
}];
}

  3.在请求数据后,服务器会返回json数据,为了面向模型开发,添加SVStatus模型和SVUser模型,并根据新浪返回数据和我们需要的数据为其添加成员变量。(在这里第一次的时候是先进行了数据解析,直接在系统的cell上展示了头像、用户名等数据,这次做就直接自定义cell)

 #import <Foundation/Foundation.h>
@class SVUser;
@interface SVStatus : NSObject
/**
* 微博创建时间
*/
@property (nonatomic, copy) NSString *created_at;
/**
* 微博ID
*/
@property (nonatomic, copy) NSString *ID;
/**
* 微博内容
*/
@property (nonatomic, copy) NSString *text;
/**
* 微博来源
*/
@property (nonatomic, copy) NSString *source;
/**
* 微博配图的缩略图地址
*/
@property (nonatomic, copy) NSString *thumbnail_pic;
/**
* 转发数
*/
@property (nonatomic, assign) int retwweted_count;
/**
* 评论数
*/
@property (nonatomic, assign) int reposts_count;
/**
* 点赞数
*/
@property (nonatomic, assign) int attitudes_count;
/**
* 发微博的人
*/
@property (nonatomic, strong) SVUser *user;
/**
* 转发的微博
*/
@property (nonatomic, strong) SVStatus *retweeted_status;
@end
 #import <Foundation/Foundation.h>

 @interface SVUser : NSObject
/**
* 用户的ID
*/
@property (nonatomic, copy) NSString *idstr;
/**
* 用户的昵称
*/
@property (nonatomic, copy) NSString *name;
/**
* 用户的头像
*/
@property (nonatomic, copy) NSString *profile_image_url; /**
* 会员等级
*/
@property (nonatomic, assign) int mbrank;
/**
* 会员类型
*/
@property (nonatomic, assign) int mbtype;
@end

  4.典型的cell高度不确定时,分析微博的结构,而微博的结构根据内容会有所不同,基本为三种:只有文字、文字和配图、带有转发的微博。不管哪种,按照从上到下的顺序,就可以将其分为三个部分:头像昵称、主要内容和转评赞条。为了确定位置,会再添加一个计算frame的模型SVStatusFrame,用来计算每一个控件在整个cell的位置。在SVStatusFrame中只需要重写status的setter方法,在给status设置值得时候,根据不同的值进行计算控件的frame,最后得出整个cell的高度。目前在SVStatusFrame的.h文件中哟很多关于字体、颜色的宏定义,还有目前对于头文件的引用也不是很规范,在基本效果完成之后会将他们进行抽取。

 @interface SVStatusFrame : NSObject
@property (nonatomic, strong) SVStatus *status; /** 顶部的view */
@property (nonatomic, assign, readonly) CGRect topViewF;
/** 头像 */
@property (nonatomic, assign, readonly) CGRect iconViewF;
/** 会员图标 */
@property (nonatomic, assign, readonly) CGRect vipViewF;
/** 配图 */
@property (nonatomic, assign, readonly) CGRect photoViewF;
/** 昵称 */
@property (nonatomic, assign, readonly) CGRect nameLabelF;
/** 时间 */
@property (nonatomic, assign, readonly) CGRect timeLabelF;
/** 来源 */
@property (nonatomic, assign, readonly) CGRect sourceLabelF;
/** 正文\内容 */
@property (nonatomic, assign, readonly) CGRect contentLabelF; /** 被转发微博的view(父控件) */
@property (nonatomic, assign, readonly) CGRect retweetViewF;
/** 被转发微博作者的昵称 */
@property (nonatomic, assign, readonly) CGRect retweetNameLabelF;
/** 被转发微博的正文\内容 */
@property (nonatomic, assign, readonly) CGRect retweetContentLabelF;
/** 被转发微博的配图 */
@property (nonatomic, assign, readonly) CGRect retweetPhotoViewF; /** 微博的工具条 */
@property (nonatomic, assign, readonly) CGRect statusToolbarF; /** cell的高度 */
@property (nonatomic, assign, readonly) CGFloat cellHeight;

  5.为了使cell不过于依赖控制器而存在,为其提供一个类方法来创建件cell,并在cell内部实现重用。在该方法中添加控件,添加控件分成了3个部分,其中的代码很简单的就是创建控件、设置只要不需改变的值、添加、保存起来。

 // 全部的控件
@interface SVStatusCell()
/** 顶部的view */
@property (nonatomic, weak) UIImageView *topView;
/** 头像 */
@property (nonatomic, weak) UIImageView *iconView;
/** 会员图标 */
@property (nonatomic, weak) UIImageView *vipView;
/** 配图 */
@property (nonatomic, weak) UIImageView *photoView;
/** 昵称 */
@property (nonatomic, weak) UILabel *nameLabel;
/** 时间 */
@property (nonatomic, weak) UILabel *timeLabel;
/** 来源 */
@property (nonatomic, weak) UILabel *sourceLabel;
/** 正文\内容 */
@property (nonatomic, weak) UILabel *contentLabel; /** 被转发微博的view(父控件) */
@property (nonatomic, weak) UIImageView *retweetView;
/** 被转发微博作者的昵称 */
@property (nonatomic, weak) UILabel *retweetNameLabel;
/** 被转发微博的正文\内容 */
@property (nonatomic, weak) UILabel *retweetContentLabel;
/** 被转发微博的配图 */
@property (nonatomic, weak) UIImageView *retweetPhotoView; /** 微博的工具条 */
@property (nonatomic, weak) UIImageView *statusToolbar;
@end
 + (instancetype)cellWithtableView:(UITableView *)tableView
{
static NSString *ID = @"status";
SVStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[SVStatusCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
return cell;
} - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// 1.添加原创微博内部的子控件
[self setupOriginalSubviews]; // 2.添加被转发微博内部的子控件
[self setupRetweetSubviews]; // 3.添加微博的工具条
[self setupStatusToolBar];
}
return self;
}

  6.对于给cell上的控件赋值数据同样很简单,就是重写statusFrame的setter方法,再给他赋值的同时,将status数据传递过来。

 /**
* 传递模型数据
*/
- (void)setStatusFrame:(SVStatusFrame *)statusFrame
{
_statusFrame = statusFrame; // 1.原创微博
[self setupOriginalData]; // 2.被转发微博
[self setupRetweetData]; // 3.微博工具条
[self setupStatusToolbar];
}

  

 

iOS实践04的更多相关文章

  1. 使用Bootstrap 3开发响应式网站实践04,使用Panels展示内容

    在Bootstrap页面中,通常用Panels来展示主要功能的内容.该部分Html为: <div class="row" id="featureHeading&qu ...

  2. iOS实践03

    主要目标:版本新特性界面,新浪授权界面(登录界面)的处理 任务基本完成了,基本的框架也就到这了,接下来的应该是首页获取微博了. 1.版本新特性,可以单独作为一个model,写完之加入到项目中.我们新建 ...

  3. Istio技术与实践04:最佳实践之教你写一个完整的Mixer Adapter

    Istio内置的部分适配器以及相应功能举例如下: circonus:微服务监控分析平台. cloudwatch:针对AWS云资源监控的工具. fluentd:开源的日志采集工具. prometheus ...

  4. AJ学IOS(04)UI之半小时搞定Tom猫

    AJ分享 必须精品  效果图 曾经风靡一时的tom猫其实制作起来那是叫一个相当的easy啊 功能全部实现,(关键是素材,没有素材的可以加我微信) 新手也可以很快的完成tom这个很拉轰的ios应用哦 然 ...

  5. iOS实践01

    去年放假之前大概完成了新浪微博项目,到现在也忘得差不多了,打算在重新写一遍.之前的一些笔记在新浪的博客SleenXiu,在这主要是把新浪微博以随笔的形式写在这,方便以后的复习. 先看看之前主要完成的几 ...

  6. iOS实践02

    第二天了,上了一天课,软件测试.数据挖掘.概率论,晚上了才有时间捣鼓捣鼓程序. 今天只是简单的做了一点.觉得自己思考的写不出来,只能简单的写一个过程,不像第一次写这个,少了很多思考的. 1.完善tab ...

  7. swift 实践- 04 -- UIButton

    import UIKit class ViewController: UIViewController { // 按钮的创建 // UIButtonType.system: 前面不带图标, 默认文字为 ...

  8. 使用Axure RP原型设计实践04,了解全局变量

    变量是一个可以变的数,可以看作是一个数据的容器.变量有2个操作,一个是读,一个是写.Axure的全局变量是指任何时候都可以对这个变量进行读写操作. 点击工具栏Project下的Global Varia ...

  9. 在ASP.NET MVC中使用Knockout实践04,控制View Model的json格式内容

    通常,需要把View Model转换成json格式传给服务端.但在很多情况下,View Model既会包含字段,还会包含方法,我们只希望把字段相关的键值对传给服务端. 先把上一篇的Product转换成 ...

随机推荐

  1. Ubuntu eclipse :An error has occurred. See the log file

    安装eclipse: sudo apt-get install eclipse-platform 调整java: sudo update-alternatives --config java 启动: ...

  2. Cloning Java objects using serialization

    Sometimes you need to clone objects, and sometimes you can't use their clone method, and sometimes s ...

  3. android加载更多的图片

    这是昨天改进后的,我测试了下,可以加载图片到5万张,估计5万以上也是没问题的,我只试到5万,其实也没必要这么高,现实中1000左右就差不多了,不过我的应用到100就差不多了, package com. ...

  4. hdu 2850 Load Balancing (优先队列 + 贪心)

    题目大意: 怎么分配n个任务到m个server上使得负载尽量平衡. 思路: 将任务从大到小排序,依次放入负载最小的那个server中. 由于是spj 的缘故,所以能够使用这个贪心. 比方数据 6 2 ...

  5. c# 针对不同数据库进行参数化查询

    使用参数化 DbCommand 的一个缺点是需要参数的代码将仅适用于支持相同语法的提供程序.OLEDB.SqlClient 和 Oracle 提供程序全部使用不同的语法.例如,用于命名和指定参数的 S ...

  6. python 调用hive查询实现类似存储过程

    需求:数据仓库中所有表的定义结构保存到新的文件中,保存后类似下面数据,重复的数据只保留7月份即可 ****************ods_log_info*****************lid st ...

  7. Infragistics的介绍以及在ASP.net中使用的总结

    Infragistics系列控件是一套很好,很强大的控件,.感觉很好..现在自己做项目也用..却发现网上没有一套中文的教程,中文资料都很少..在这里就把自己的研究心得写下来... 首先安装,一步一步装 ...

  8. 基础知识——Cocos2d-x学习历程(三)

    1.场景与流程控制 我们把一些内容相对不变的游戏元素集合称作场景(scene),把游戏在场景之间切换的过程叫做流程控制(flow control). 在Cocos2d-x中,场景的实现是Scene. ...

  9. easyui给select控件绑定change事件

    一般的做法是使用jQuery来绑定,例如: $("#id").change(function(){ alert("change事件绑定"); }); 当给sel ...

  10. Android 汉字转拼音之工具篇

    /* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Versi ...