分享

ios实战开发之仿新浪微博(第六讲:微博数据展示一)

 wintelsui 2014-11-25
 1、效果演示



2、设计说明

2.0 无相关基础请先参照前面几讲的内容
2.1 设计框架模型SAStatusFrame类,用来保存微博内容及各控件尺寸与位置,参考链接:ios实战开发之代码创建Cell(演示新浪微博)
2.2 修改SAStatusTool类,修改数据模型数组(之前数据模型数组保存的对象为SAStatus模型,现保存SAStatusFrame模型)
2.3 设计SAStatueCell类,自定义TableViewCell,参考链接:ios实战开发之代码创建Cell(演示新浪微博)
2.4 数据模型初始化之后已将各Cell的尺寸位置及内容均已计算好,因此,直接将取数据模型中的值即可轻松准确展示各Cell的值及视图
2.5 参考模型图:


3、关键代码
SAStatus.h
  1. //
  2. //  SAStatus.h
  3. //  SianWeibo
  4. //
  5. //  Created by yusian on 14-4-16.
  6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
  7. //

  8. #import <Foundation/Foundation.h>
  9. #import "SAStatusUser.h"

  10. @interface SAStatus : NSObject

  11. @property (nonatomic, copy)     NSString        *text;              // 正文
  12. @property (nonatomic, strong)   SAStatusUser    *user;              // 用户
  13. @property (nonatomic, copy)     NSString        *createdAt;         // 创建时间
  14. @property (nonatomic, copy)     NSString        *source;            // 来源
  15. @property (nonatomic, strong)   SAStatus        *retweetedStatus;   // 转发体
  16. @property (nonatomic, assign)   NSInteger       repostsCount;       // 转发数
  17. @property (nonatomic, assign)   NSInteger       commentsCount;      // 评论数
  18. @property (nonatomic, assign)   NSInteger       attitudesCount;     // 点赞数
  19. @property (nonatomic, strong)   NSArray         *picUrls;           // 配图

  20. - (id)initWithDict:(NSDictionary *)dict;

  21. + (id)statusWithDict:(NSDictionary *)dict;

  22. @end
复制代码

SAStatus.m
  1. //
  2. //  SAStatus.m
  3. //  SianWeibo
  4. //
  5. //  Created by yusian on 14-4-16.
  6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
  7. //

  8. #import "SAStatus.h"

  9. @implementation SAStatus

  10. - (id)initWithDict:(NSDictionary *)dict
  11. {
  12.     if (self = [super init]) {
  13.         self.text = dict[@"text"];                                      // 正文
  14.         self.user = [SAStatusUser statusUserWithDict:dict[@"user"]];    // 用户
  15.         self.createdAt = dict[@"created_at"];                           // 创建时间
  16.         self.source = dict[@"source"];                                  // 来源
  17.         self.repostsCount = [dict[@"reposts_count"] intValue];          // 转发数
  18.         self.commentsCount = [dict[@"comments_count"] intValue];        // 评论数
  19.         self.attitudesCount = [dict[@"attitudes_count"] intValue];      // 点赞数
  20.         self.picUrls = dict[@"pic_urls"];                               // 配图
  21.         
  22.         NSDictionary *retweetedStatus = dict[@"retweeted_status"];
  23.         if (retweetedStatus) {                                          // 转发体(被转载的微博内容)
  24.             
  25.             self.retweetedStatus = [[SAStatus alloc] initWithDict:retweetedStatus];
  26.             
  27.         }
  28.     }
  29.     return self;
  30. }

  31. + (id)statusWithDict:(NSDictionary *)dict
  32. {
  33.     return [[self alloc] initWithDict:dict];
  34. }

  35. @end
复制代码

SAStatusUser.h
  1. //
  2. //  SAStatusUser.h
  3. //  SianWeibo
  4. //
  5. //  Created by yusian on 14-4-16.
  6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
  7. //

  8. #import <Foundation/Foundation.h>

  9. @interface SAStatusUser : NSObject

  10. @property (nonatomic, copy)     NSString    *screenName;        // 昵称
  11. @property (nonatomic, copy)     NSString    *profileImageUrl;   // 头像
  12. @property (nonatomic, assign)   BOOL        verified;           // 是否验证
  13. @property (nonatomic, assign)   NSInteger   verifiedType;       // 验证类型
  14. @property (nonatomic, assign)   NSInteger   mbrank;             // 会员等级
  15. @property (nonatomic, assign)   NSInteger   mbtype;             // 会员类型

  16. - (id)initWithDict:(NSDictionary *)dict;

  17. + (id)statusUserWithDict:(NSDictionary *)dict;

  18. @end
复制代码

SAStatusUser.m
  1. //
  2. //  SAStatusUser.m
  3. //  SianWeibo
  4. //
  5. //  Created by yusian on 14-4-16.
  6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
  7. //

  8. #import "SAStatusUser.h"

  9. @implementation SAStatusUser

  10. - (id)initWithDict:(NSDictionary *)dict
  11. {
  12.     if (self = [super init]) {
  13.         self.screenName = dict[@"screen_name"];                     // 昵称
  14.         self.profileImageUrl = dict[@"profile_image_url"];          // 头像
  15.         self.verified = [dict[@"verified"] boolValue];              // 是否验证
  16.         self.verifiedType = [dict[@"verified_type"] integerValue];  // 验证类型
  17.         self.mbrank = [dict[@"mbrank"] integerValue];               // 会员等级
  18.         self.mbtype = [dict[@"mbtype"] integerValue];               // 会员类型
  19.     }
  20.     return self;
  21. }

  22. + (id)statusUserWithDict:(NSDictionary *)dict
  23. {
  24.     return [[self alloc] initWithDict:dict];
  25. }

  26. @end
复制代码

SAStatusFrame.h
  1. //
  2. //  SAStatusFrame.h
  3. //  SianWeibo
  4. //
  5. //  Created by yusian on 14-4-18.
  6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
  7. //

  8. #import <Foundation/Foundation.h>
  9. #import "SAStatus.h"
  10. #define kInterval 10
  11. #define kProfileWH 40
  12. #define kScreenNameFount [UIFont systemFontOfSize:16]
  13. #define kTimeFont [UIFont systemFontOfSize:13]
  14. #define kSourceFont kTimeFont
  15. #define kTextFount [UIFont systemFontOfSize:15]
  16. #define kReScreenNameFont [UIFont systemFontOfSize:14]
  17. #define kReTextFont kReScreenNameFont

  18. @interface SAStatusFrame : NSObject

  19. @property (nonatomic, readonly) CGRect      profile;        // 头像
  20. @property (nonatomic, readonly) CGRect      screenName;     // 昵称
  21. @property (nonatomic, readonly) CGRect      time;           // 时间
  22. @property (nonatomic, readonly) CGRect      source;         // 来源
  23. @property (nonatomic, readonly) CGRect      text;           // 正文
  24. @property (nonatomic, readonly) CGRect      image;          // 配图
  25. @property (nonatomic, readonly) CGRect      retweet;        // 转发体视图
  26. @property (nonatomic, readonly) CGRect      reScreenName;   // 转发体昵称
  27. @property (nonatomic, readonly) CGRect      reText;         // 转发体正文
  28. @property (nonatomic, readonly) CGRect      reImage;        // 转发体配图

  29. @property (nonatomic, readonly) CGFloat     cellHeight;     // 行高
  30. @property (nonatomic, strong)   SAStatus    *status;        // 数据模型



  31. @end
复制代码

SAStatusFrame.m
  1. //
  2. //  SAStatusFrame.m
  3. //  SianWeibo
  4. //
  5. //  Created by yusian on 14-4-18.
  6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
  7. //

  8. #import "SAStatusFrame.h"

  9. @implementation SAStatusFrame

  10. -(void)setStatus:(SAStatus *)status
  11. {
  12.     _status = status;
  13.     CGSize screenSize = [UIScreen mainScreen].applicationFrame.size;
  14.    
  15.     // 1、设置头像尺寸位置;
  16.     CGFloat profileX = kInterval;
  17.     CGFloat profileY = kInterval;
  18.     _profile = CGRectMake(profileX, profileY, kProfileWH, kProfileWH);
  19.    
  20.     // 2、设置昵称尺寸位置;
  21.     CGFloat screenNameX = CGRectGetMaxX(_profile) + kInterval;
  22.     CGFloat screenNameY = profileY;
  23.     CGSize screenNameSize = [status.user.screenName sizeWithFont:kScreenNameFount];
  24.     _screenName = (CGRect){{screenNameX, screenNameY}, screenNameSize};
  25.    
  26.     // 3、设置时间尺寸位置
  27.     CGFloat timeX = screenNameX;
  28.     CGFloat timeY = CGRectGetMaxY(_screenName);
  29.     CGSize timeSize = [status.createdAt sizeWithFont:kTimeFont];
  30.     _time = (CGRect){{timeX, timeY}, timeSize};
  31.    
  32.     // 4、设置来源尺寸位置
  33.     CGFloat sourceX = CGRectGetMaxX(_time) + kInterval;
  34.     CGFloat sourceY = timeY;
  35.     CGSize sourceSize = [status.source sizeWithFont:kSourceFont];
  36.     _source = (CGRect){{sourceX, sourceY}, sourceSize};
  37.    
  38.     // 5、设置正文尺寸位置;
  39.     CGFloat textX = profileX;
  40.     CGFloat textY = MAX (CGRectGetMaxY(_profile), CGRectGetMaxY(_time)) + kInterval;
  41.     CGFloat textW = screenSize.width - 2 * kInterval;
  42.     CGSize textSize = [_status.text sizeWithFont:kTextFount constrainedToSize:CGSizeMake(textW, MAXFLOAT)];
  43.     _text = (CGRect){{textX, textY}, textSize};
  44.    
  45.     if (_status.picUrls.count) {                            // 第一种情况:带配图的微博
  46.         
  47.         // 6、设置配图尺寸位置
  48.         CGFloat imageX = profileX;
  49.         CGFloat imageY = CGRectGetMaxY(_text) + kInterval;
  50.         CGSize imageSize = {100, 100};
  51.         _image = (CGRect){{imageX, imageY}, imageSize};
  52.         
  53.         // 有配图无转发体单元格高度
  54.         _cellHeight = CGRectGetMaxY(_image) + kInterval;
  55.         
  56.     } else if (_status.retweetedStatus) {                   // 第二种情况:转发的微博
  57.         
  58.         // 7、设置转发体尺寸位置
  59.         CGFloat retweetX = profileX;
  60.         CGFloat retweetY = CGRectGetMaxY(_text) + kInterval;
  61.         CGFloat retweetW = screenSize.width - 2 * kInterval;
  62.         CGFloat retweetH = kInterval;
  63.         _retweet = CGRectMake(retweetX, retweetY, retweetW, retweetH);
  64.         
  65.         // 8、设置转发体昵称尺寸位置
  66.         CGFloat reScreenNameX = kInterval;
  67.         CGFloat reScreenNameY = kInterval;
  68.         CGSize reScreenNameSize = [[NSString stringWithFormat:@"@%@", _status.retweetedStatus.user.screenName] sizeWithFont:kReScreenNameFont];
  69.         _reScreenName = (CGRect){{reScreenNameX, reScreenNameY}, reScreenNameSize};
  70.         
  71.         // 9、设置转发体正文尺寸位置
  72.         CGFloat reTextX = reScreenNameX;
  73.         CGFloat reTextY = CGRectGetMaxY(_reScreenName) + kInterval;
  74.         CGSize reTextSize = [_status.retweetedStatus.text sizeWithFont:kReTextFont constrainedToSize:CGSizeMake((screenSize.width - 4 * kInterval), MAXFLOAT)];
  75.         _reText = (CGRect){{reTextX, reTextY}, reTextSize};
  76.         
  77.         // 10、设置转发体配图尺寸位置
  78.         if (_status.retweetedStatus.picUrls) {              // 第二种情况:1、转发的微博带图
  79.             CGFloat reImageX = reScreenNameX;
  80.             CGFloat reImageY = CGRectGetMaxY(_reText) + kInterval;
  81.             CGSize reImageSize = {100, 100};
  82.             _reImage = (CGRect){{reImageX, reImageY}, reImageSize};
  83.             
  84.             // 转发体有配图转发体尺寸
  85.             retweetH = CGRectGetMaxY(_reImage) + kInterval;
  86.             _retweet = CGRectMake(retweetX, retweetY, retweetW, retweetH);
  87.             
  88.         } else {                                            // 第二种情况:2、转发的微博不带图
  89.             
  90.             // 转发体无配图转发体尺寸
  91.             retweetH = CGRectGetMaxY(_reText) + kInterval;
  92.             _retweet = CGRectMake(retweetX, retweetY, retweetW, retweetH);
  93.         }
  94.         
  95.         // 有转发体的单元格高度
  96.         _cellHeight = CGRectGetMaxY(_retweet) + kInterval;
  97.         
  98.     } else {                                                // 第三种情况:不带配图的普通微博
  99.         
  100.         // 11、设置单元格高度尺寸位置
  101.         // 无配图,无转发体单元格高度
  102.         _cellHeight = CGRectGetMaxY(_text) + kInterval;
  103.     }
  104. }

  105. @end
复制代码

SAStatusTool.m
  1. //
  2. //  SAStatusTool.m
  3. //  SianWeibo
  4. //
  5. //  Created by yusian on 14-4-16.
  6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
  7. //

  8. #import "SAStatusTool.h"
  9. #import "SAStatusFrame.h"
  10. #import "SAHttpTool.h"

  11. @implementation SAStatusTool

  12. + (void)statusToolGetStatusSuccess:(StatusSuccess)success failurs:(StatusFailurs)failure
  13. {
  14.     [[self alloc] initWithHttpToolStatusSuccess:success failurs:failure];
  15. }

  16. - (void)initWithHttpToolStatusSuccess:(StatusSuccess)success failurs:(StatusFailurs)failure
  17. {
  18.     // 调用SAHttpTool这个工具类,发送相关请求返回数组内容
  19.     [SAHttpTool httpToolPostWithBaseURL:kBaseURL path:@"2/statuses/home_timeline.json" params:
  20.      @{
  21.        @"count" : @"40"
  22.       
  23.        } success:^(id JSON) {
  24.          
  25.          // 如果方法调用没有实现success部分,则方法直接返回
  26.          if (success == nil) return;
  27.          
  28.          // 1、将返回的JSON转换成微博模型并保存到数组
  29.          NSMutableArray *statuses = [NSMutableArray array];
  30.          for (NSDictionary *dict in JSON[@"statuses"]) {
  31.             
  32.              // 将JSON中解析的数据保存到框架模型中
  33.              SAStatusFrame *statusFrame = [[SAStatusFrame alloc] init];
  34.              statusFrame.status = [SAStatus statusWithDict:dict];
  35.              [statuses addObject:statusFrame];
  36.          }
  37.          
  38.          // 2、将数组返回给Block形参供方法调用者使用
  39.          success(statuses);
  40.          
  41.      } failure:^(NSError *error) {
  42.          
  43.          if (failure == nil) return;
  44.          
  45.          failure(error);
  46.          
  47.      } method:@"GET"];
  48.    
  49. }
  50. @end
复制代码

SAStatusCell.h
  1. //
  2. //  SAStatusCell.h
  3. //  SianWeibo
  4. //
  5. //  Created by yusian on 14-4-18.
  6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
  7. //

  8. #import <UIKit/UIKit.h>
  9. #import "SAStatusFrame.h"

  10. @interface SAStatusCell : UITableViewCell

  11. @property (nonatomic, strong) SAStatusFrame *statusFrame;

  12. + (NSString *)ID;

  13. @end
复制代码

SAStatusCell.m
  1. //
  2. //  SAStatusCell.m
  3. //  SianWeibo
  4. //
  5. //  Created by yusian on 14-4-18.
  6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
  7. //

  8. #import "SAStatusCell.h"
  9. #import "UIImageView+WebCache.h"

  10. @interface SAStatusCell ()
  11. {
  12.     UIImageView *_profile;      // 头像
  13.     UILabel     *_screenName;   // 昵称
  14.     UILabel     *_time;         // 时间
  15.     UILabel     *_source;       // 来源
  16.     UILabel     *_text;         // 正文
  17.     UIImageView *_image;        // 配图
  18.     UIImageView *_retweet;      // 转发体视图
  19.     UILabel     *_reScreenName; // 转发体昵称
  20.     UILabel     *_reText;       // 转发体正文
  21.     UIImageView *_reImage;      // 转发体配图
  22. }
  23. @end

  24. @implementation SAStatusCell

  25. #pragma mrak 初始化单元格元素
  26. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  27. {
  28.     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  29.     if (self) {
  30.         
  31.         // 1、头像
  32.         _profile = [[UIImageView alloc] init];
  33.         [self.contentView addSubview:_profile];
  34.         
  35.         // 2、昵称
  36.         _screenName = [[UILabel alloc] init];
  37.         _screenName.font = kScreenNameFount;
  38.         [self.contentView addSubview:_screenName];
  39.         
  40.         // 3、时间
  41.         _time = [[UILabel alloc] init];
  42.         _time.font = kTimeFont;
  43.         [self.contentView addSubview:_time];
  44.         
  45.         // 4、来源
  46.         _source = [[UILabel alloc] init];
  47.         _source.font = kSourceFont;
  48.         [self.contentView addSubview:_source];
  49.         
  50.         // 5、正文
  51.         _text = [[UILabel alloc] init];
  52.         _text.font = kTextFount;
  53.         _text.numberOfLines = 0;
  54.         [self.contentView addSubview:_text];
  55.         
  56.         // 6、配图
  57.         _image = [[UIImageView alloc] init];
  58.         _image.contentMode = UIViewContentModeScaleAspectFit;
  59.         [self.contentView addSubview:_image];
  60.         
  61.         // 7、转发体视图
  62.         _retweet = [[UIImageView alloc] init];
  63.         _retweet.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
  64.         [self.contentView addSubview:_retweet];
  65.         
  66.         // 8、转发体昵称
  67.         _reScreenName = [[UILabel alloc] init];
  68.         _reScreenName.font = kReScreenNameFont;
  69.         _reScreenName.backgroundColor = [UIColor clearColor];
  70.         _reScreenName.textColor = [UIColor blueColor];
  71.         [_retweet addSubview:_reScreenName];
  72.         
  73.         // 9、转发体正文
  74.         _reText = [[UILabel alloc] init];
  75.         _reText.numberOfLines = 0;
  76.         _reText.font = kReTextFont;
  77.         _reText.backgroundColor = [UIColor clearColor];
  78.         [_retweet addSubview:_reText];
  79.         
  80.         // 10、转发体配图
  81.         _reImage = [[UIImageView alloc] init];
  82.         _reImage.contentMode = UIViewContentModeScaleAspectFit;
  83.         [_retweet addSubview:_reImage];
  84.         
  85.     }
  86.     return self;
  87. }

  88. #pragma mark - 设置单元格
  89. -(void)setStatusFrame:(SAStatusFrame *)statusFrame
  90. {
  91.     _statusFrame = statusFrame;
  92.    
  93.     [self statusFrameSettingView];
  94.    
  95.     [self statusFrameSettingFrame];
  96. }

  97. #pragma mark 设置单元格内容
  98. - (void)statusFrameSettingView
  99. {
  100.     SAStatus *status = self.statusFrame.status;
  101.    
  102.     // 1、设置头像
  103.     [_profile setImageWithURL:[NSURL URLWithString:status.user.profileImageUrl]
  104.              placeholderImage:[UIImage imageNamed:@"Icon.png"]
  105.                       options:SDWebImageRetryFailed | SDWebImageLowPriority];
  106.    
  107.     // 2、设置昵称
  108.     _screenName.text = status.user.screenName;
  109.    
  110.     // 3、设置时间
  111.     _time.text = status.createdAt;
  112.    
  113.     // 4、设置来源
  114.     _source.text = status.source;
  115.    
  116.     // 5、设置正文
  117.     _text.text = status.text;
  118.    
  119.     // 6、设置配图
  120.     if (status.picUrls.count) {                     // 第一种情况:带有配图的微博
  121.         
  122.         _image.hidden = NO;
  123.         _retweet.hidden = YES;
  124.         
  125.         [_image setImageWithURL:[NSURL URLWithString:status.picUrls[0][@"thumbnail_pic"]] placeholderImage:[UIImage imageNamed:@"Icon.png"] options:SDWebImageLowPriority | SDWebImageRetryFailed];
  126.         
  127.     } else if (status.retweetedStatus) {            // 第二种情况:转发的微博
  128.         
  129.         _image.hidden = YES;
  130.         _retweet.hidden = NO;
  131.         
  132.         // 7、设置转发体昵称
  133.         _reScreenName.text = [NSString stringWithFormat:@"@%@", status.retweetedStatus.user.screenName];
  134.         
  135.         // 8、转发体正文
  136.         _reText.text = status.retweetedStatus.text;
  137.         
  138.         // 9、转发体配图
  139.         if (status.retweetedStatus.picUrls.count) { // 第二种情况:1、转发的微博带配图
  140.             
  141.             // 设置转发体图片内容(暂时取一张为例)
  142.             _reImage.hidden = NO;
  143.             [_reImage setImageWithURL:[NSURL URLWithString:status.retweetedStatus.picUrls[0][@"thumbnail_pic"]] placeholderImage:[UIImage imageNamed:@"Icon.png"] options:SDWebImageLowPriority | SDWebImageRetryFailed];
  144.             
  145.         } else {                                    // 第二种情况:2、转发的微博不带配图
  146.             
  147.             // 无配图则清空属性并隐藏
  148.             _reImage.hidden = YES;
  149.         }
  150.         
  151.     } else {                                        // 第三种情况:不带配图的微博
  152.         
  153.         _image.hidden = YES;
  154.         _retweet.hidden = YES;
  155.     }

  156. }

  157. #pragma mark 单元格元素布局
  158. - (void)statusFrameSettingFrame
  159. {
  160.     // 1、设置头像尺寸位置
  161.     _profile.frame = _statusFrame.profile;
  162.    
  163.     // 2、设置昵称尺寸位置
  164.     _screenName.frame = _statusFrame.screenName;
  165.    
  166.     // 3、设置时间尺寸位置
  167.     _time.frame = _statusFrame.time;
  168.    
  169.     // 4、设置来源尺寸位置
  170.     _source.frame = _statusFrame.source;
  171.    
  172.     // 5、设置正文尺寸位置
  173.     _text.frame = _statusFrame.text;
  174.    
  175.     // 6、设置配图尺寸位置
  176.     _image.frame = _statusFrame.image;
  177.    
  178.     // 7、设置转发体尺寸位置
  179.     _retweet.frame = _statusFrame.retweet;
  180.    
  181.     // 8、设置转发体昵称尺寸位置
  182.     _reScreenName.frame = _statusFrame.reScreenName;
  183.    
  184.     // 9、转发体正文尺寸位置
  185.     _reText.frame = _statusFrame.reText;
  186.    
  187.     // 10、转发体配图尺寸位置
  188.     _reImage.frame = _statusFrame.reImage;

  189. }

  190. #pragma mark 设置单元格标识
  191. + (NSString *)ID
  192. {
  193.     return @"StatusCell";
  194. }

  195. @end
复制代码

SAHomeController.m
  1. //
  2. //  SAHomeController.m
  3. //  SianWeibo
  4. //
  5. //  Created by yusian on 14-4-12.
  6. //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
  7. //  首页控制器

  8. #import "SAHomeController.h"
  9. #import "NSString+SA.h"
  10. #import "UIBarButtonItem+SA.h"
  11. #import "SAStatusTool.h"
  12. #import "UIImageView+WebCache.h"
  13. #import "SAStatusCell.h"


  14. @interface SAHomeController ()
  15. {
  16.     NSArray         *_statusFrame;  // 框架模型数组
  17. }

  18. @end

  19. @implementation SAHomeController

  20. #pragma mark - 初始化方法
  21. - (id)initWithStyle:(UITableViewStyle)style
  22. {
  23.     self = [super initWithStyle:style];
  24.     if (self) {
  25.         // Custom initialization
  26.     }
  27.     return self;
  28. }

  29. #pragma mark - 界面内容展示
  30. - (void)viewDidLoad
  31. {
  32.     [super viewDidLoad];
  33.    
  34.     // self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  35.    
  36.     _statusFrame = [NSMutableArray array];
  37.    
  38.     // 1、设置基本界面
  39.     [self loadBasicUI];
  40.    
  41.     // 2、加载数据
  42.     [self loadStatusData];
  43.    
  44. }

  45. #pragma mark 加载基本界面
  46. - (void)loadBasicUI
  47. {
  48.    
  49.     self.title = @"首页";
  50.    
  51.     // 用自定的分类方法给导航条添加左边按钮
  52.     self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImageName:@"navigationbar_compose.png" highLightedImageName:@"navigationbar_compose_highlighted.png" addTarget:self action:@selector(leftButtonClick) forControlEvents:UIControlEventTouchUpInside];
  53.    
  54.     // 用自定的分类方法给导航条添加右边按钮
  55.     self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithImageName:@"navigationbar_pop.png" highLightedImageName:@"navigationbar_pop_highlighted.png" addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
  56.    
  57. }

  58. #pragma mark 加载微博数据
  59. - (void)loadStatusData
  60. {
  61.     // 调用SAStatusTool方法直接加载数据到模型数组
  62.     [SAStatusTool statusToolGetStatusSuccess:^(NSArray *array) {
  63.         
  64.         _statusFrame = [NSArray arrayWithArray:array];
  65.         [self.tableView reloadData];
  66.         
  67.     } failurs:^(NSError *error) {
  68.         
  69.         MyLog(@"%@", [error localizedDescription]);
  70.         
  71.     }];
  72.    
  73. }

  74. #pragma mark - 单元格属性
  75. #pragma mark 总单元格行数
  76. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  77. {
  78.     // 框架模型个数即为单元格数
  79.     return _statusFrame.count;
  80. }

  81. #pragma mark 单元格内容
  82. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  83. {
  84.     SAStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:[SAStatusCell ID]];
  85.    
  86.     if (cell == nil){
  87.         cell = [[SAStatusCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[SAStatusCell ID]];
  88.     }
  89.    
  90.     // 单元格内容由框架模型提供
  91.     cell.statusFrame = _statusFrame[indexPath.row];
  92.    
  93.     return cell;
  94. }

  95. #pragma mark 单元格高度
  96. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  97. {
  98.     // 取对应框架模型中的单元格高度属性
  99.     return [_statusFrame[indexPath.row] cellHeight];
  100. }

  101. #pragma mark - 按钮事件处理
  102. #pragma mark 首页导航左按钮事件
  103. - (void)leftButtonClick
  104. {
  105.     MyLog(@"首页左按钮");
  106. }

  107. #pragma mark 首页导航右按钮事件
  108. - (void)rightButtonClick
  109. {
  110.     MyLog(@"首页右按钮");
  111. }

  112. @end
复制代码

4、源码下载
wintelsui,如果您要查看本帖隐藏内容请回复

相关主题链接
1、ios实战开发之仿新浪微博(第一讲:新特性展示)
2、ios实战开发之仿新浪微博(第二讲:主框架搭建)
3、ios实战开发之仿新浪微博(第三讲:更多界面搭建)
4、ios实战开发之仿新浪微博(第四讲:OAuth认证)
5、ios实战开发之仿新浪微博(第五讲:微博数据加载)
6、ios实战开发之仿新浪微博(第六讲:微博数据展示一)
7、ios实战开发之仿新浪微博(第七讲:微博数据展示二)
8、ios实战开发之仿新浪微博(第八讲:微博数据展示三)
9、ios实战开发之仿新浪微博(第九讲:微博功能完善一)
10、ios实战开发之仿新浪微博(第十讲:微博功能完善二)
11、ios实战开发之仿新浪微博(第十一讲:微博功能完善三)
12、ios实战开发之仿新浪微博(小龙虾发布版)

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多