Sian 发表于 2014-4-22 01:42:51

ios实战开发之仿新浪微博(第九讲:微博功能完善一)

1、效果演示

http://player.youku.com/player.php/sid/XNzAyMDQyMzA0/v.swf

2、更新说明

2.1、增加下拉刷新功能
2.2、增加上拉加载更多功能
2.3、增加显示刷新微博条数提示功能
2.4、增加功能菜单相关数据显示功能

3、设计说明

3.1 下拉刷新及上拉加载更多引用第三方框架MJRefresh
3.1.1 下拉刷新加载比当前第一次条微博更新的微博数据添加到数据模型数组最前端并刷新Table数据重新展示
3.1.2 下拉加载更多加载比当前展示的最后一条微博更早的数据添加到数据模型的末端,并刷新Table数据重新展示
3.2 下拉刷新如果有更新数据,通过弹出提示栏将当前更新的信息展示出来,并设计淡入淡出提示框效果
3.3 将评论、转发、赞数据内容展示到功能菜单栏中,显示当前相关数据

4、关键代码
SAHomeController.h
//
//SAHomeController.h
//SianWeibo
//
//Created by yusian on 14-4-12.
//Copyright (c) 2014年 小龙虾论坛. All rights reserved.
//首页控制器

#import <UIKit/UIKit.h>

@interface SAHomeController : UITableViewController

@end

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

#import "SAHomeController.h"
#import "NSString+SA.h"
#import "UIBarButtonItem+SA.h"
#import "SAStatusTool.h"
#import "SAStatusCell.h"
#import "MJRefresh.h"
#import "UIImage+SA.h"


@interface SAHomeController () <MJRefreshBaseViewDelegate>
{
    NSMutableArray          *_statusFrameArray;// 框架模型数组
    MJRefreshBaseView       *_head;
}

@end

@implementation SAHomeController

#pragma mark - 初始化方法
- (id)initWithStyle:(UITableViewStyle)style
{
    self = ;
    if (self) {
      
      self.view.backgroundColor = kBGColor;
      
    }
    return self;
}

#pragma mark - 界面内容展示
- (void)viewDidLoad
{
    ;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
   
    _statusFrameArray = ;
   
    // 1、设置基本界面
    ;
   
    // 2、添加上拉下拉刷新控件
    MJRefreshHeaderView *head = ;
    head.scrollView = self.tableView;
    head.delegate = self;
    _head = head;
   
    MJRefreshFooterView *foot = ;
    foot.scrollView = self.tableView;
    foot.delegate = self;
   
    // 3、自动刷新加载数据
    ;
}

#pragma mark 加载基本界面
- (void)loadBasicUI
{
   
    self.title = @"首页";
   
    // 用自定的分类方法给导航条添加左边按钮
    self.navigationItem.leftBarButtonItem = ;
   
    // 用自定的分类方法给导航条添加右边按钮
    self.navigationItem.rightBarButtonItem = ;
   
}

#pragma mark - 自动刷新加载数据
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
   
    if (]) {
      
      // 下拉操作加载最新数据
      ;
      
    } else {
      
      // 上拉操作加载更多数据
      ;
    }
}

#pragma mark 下拉操作加载最新数据
- (void) loadNewStatus:(MJRefreshBaseView *)refreshView
{
    // 1、取出首个模型数据
    SAStatusFrame *tempStatus = ;
    long long firstStatusID = tempStatus.status.ID;
   
    // 2、发送请求,请求比首个模型数据更新的数据内容
    [SAStatusTool statusToolGetStatusWithSinceID:firstStatusID maxID:0 Success:^(NSArray *array) {
      
      // 3、将新请求到的数据添加到数据模型前端
      NSArray *newFrame = ;
      ];
      
      // 4、刷新数据,停止刷新
      ;
      ;
      
      // 5、显示提示
      if (newFrame.count) ;
      
    } failurs:^(NSError *error) {
      
      ;    // 停止刷新
      
      MyLog(@"%@", );
      
    }];
   
}

#pragma mark 显示上拉刷新结果
- (void)showNewStatusMessage:(NSUInteger)count
{
    // 1、创建按钮设置基本属性
    UIButton *msgButton = ;
    msgButton.enabled = NO;
    msgButton.adjustsImageWhenDisabled = NO;
    NSString *title = ;
    msgButton.titleLabel.font = ;
    ;
    forState:UIControlStateNormal];
    ;
   
    // 2、设置显示动画
    CGFloat height = 44;
    CGFloat y = 20;
    if ([[ systemVersion] floatValue] < 7.0) { // ios7.0以下版本包含状态栏
      y = 0;
    }
    msgButton.frame = CGRectMake(0, y, self.tableView.frame.size.width, height);
    msgButton.alpha = 0;
   
    [UIView animateWithDuration:0.5 animations:^{   // 半秒时间淡出效果
      
      msgButton.alpha = 0.9;
      msgButton.transform = CGAffineTransformTranslate(msgButton.transform, 0, height);
      
    } completion:^(BOOL finished) {               // 停留1.5秒后弹回
      [UIView animateWithDuration:1.0 delay:1.5 options:UIViewAnimationOptionCurveEaseOut animations:^{
            
            msgButton.transform = CGAffineTransformTranslate(msgButton.transform, 0, -height);
            msgButton.alpha = 0;
            
      } completion:^(BOOL finished) {             // 动画结束移除控件
            ;
      }];
    }];
   
   
}

#pragma mark 上拉操作加载更多数据
- (void)loadMoreStatus:(MJRefreshBaseView *)refreshView
{
   
    SAStatusFrame *tempStatus = ;
    long long lastStatusID = tempStatus.status.ID;
   
    // 调用SAStatusTool方法直接加载数据到模型数组
    [SAStatusTool statusToolGetStatusWithSinceID:0 maxID:lastStatusID - 1 Success:^(NSArray *array) {
      
      NSArray *newFrame = ;
      
      ;
      
      ;    // 加载数据后刷新表格
      
      ;    // 停止刷新
      
    } failurs:^(NSError *error) {
      
      ;    // 停止刷新
      
      MyLog(@"%@", );
      
    }];

}


#pragma mark - 单元格属性
#pragma mark 总单元格行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // 框架模型个数即为单元格数
    return _statusFrameArray.count;
}

#pragma mark 单元格内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SAStatusCell *cell = ];
   
    if (cell == nil){
      cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:];
    }
   
    // 单元格内容由框架模型提供
    cell.statusFrame = _statusFrameArray;
   
    return cell;
}

#pragma mark 单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 取对应框架模型中的单元格高度属性
    return cellHeight];
}


#pragma mark - 按钮事件处理
#pragma mark 首页导航左按钮事件
- (void)leftButtonClick
{
    MyLog(@"首页左按钮");
}

#pragma mark 首页导航右按钮事件
- (void)rightButtonClick
{
    MyLog(@"首页右按钮");
}

@end

SAStatusDock.h
//
//SAStatusDock.h
//SianWeibo
//
//Created by yusian on 14-4-20.
//Copyright (c) 2014年 小龙虾论坛. All rights reserved.
//微博Dock

#import <UIKit/UIKit.h>
@class SAStatus;
@interface SAStatusDock : UIImageView

@property (nonatomic, strong) SAStatus *status;

@end

SAStatusDock.m
//
//SAStatusDock.m
//SianWeibo
//
//Created by yusian on 14-4-20.
//Copyright (c) 2014年 小龙虾论坛. All rights reserved.
//微博Dock

#import "SAStatusDock.h"
#import "UIImage+SA.h"
#import "NSString+SA.h"
#import "SAStatus.h"

@interface SAStatusDock ()
{
    UIButton    *_reposts;
    UIButton    *_comments;
    UIButton    *_attitudes;
}
@end

@implementation SAStatusDock

- (id)initWithFrame:(CGRect)frame
{
    self = ;
    if (self) {
      
      // 设置Dock的尺寸位置
      CGFloat dockWidth = .bounds.size.width - 2 * kCellMargins;
      self.frame = CGRectMake(0, kCellDefaultHeight - kCellMargins - kStatusDockHeight, dockWidth, kStatusDockHeight);
      
      // Dock贴紧父控件底部,即保持在Cell底部
      self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
      
      // 接受用户交互
      self.userInteractionEnabled = YES;
      
      // 添加3个按钮
      _reposts = [self addButtonWithImage:@"timeline_icon_comment.png"
                            backgroundImage:@"timeline_card_leftbottom.png" buttonIndex:0];
      
      _comments = [self addButtonWithImage:@"timeline_icon_retweet.png"
                           backgroundImage:@"timeline_card_middlebottom.png" buttonIndex:1];
      
      _attitudes = [self addButtonWithImage:@"timeline_icon_unlike.png"
                              backgroundImage:@"timeline_card_rightbottom.png" buttonIndex:2];
    }
    return self;
}

#pragma mark 添加功能菜单栏按钮
- (UIButton *)addButtonWithImage:(NSString *)imageName backgroundImage:(NSString *)backgroundImageName buttonIndex:(NSInteger)index
{
    // 按钮基本属性设置
    UIButton *button = ;
    forState:UIControlStateNormal];       // 文字颜色
    button.titleLabel.font = ;                        // 文字大小
    forState:UIControlStateNormal]; // 按钮图标
    button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);                         // 图文间距
   
    // 按钮背景图片
    forState:UIControlStateNormal];
    ] forState:UIControlStateHighlighted];
   
    // 按钮尺寸位置
    CGFloat buttonWidth = self.frame.size.width / 3;
    button.frame = CGRectMake(index * buttonWidth, 0, buttonWidth, kStatusDockHeight);
   
    // 添加按钮间间隔图片
    if (index) {
      UIImageView *cardButton = [ initWithImage:];
      ;
      cardButton.center = CGPointMake(button.frame.origin.x, kStatusDockHeight * 0.5);
    }
    ;
    return button;
}

#pragma mark - 模型数据传递
-(void)setStatus:(SAStatus *)status
{
    _status = status;
   
    // 根据模型数据内容设置菜单栏按钮文字
    ;
    ;
    ;
}

#pragma mark 设置功能菜单栏数据
// 如果有数值,将数值替代文字
- (void)setButton:(UIButton *)button withTitle:(NSString *)title forCounts:(NSUInteger)number
{
    if (number > 10000) {       // 一万条以上数据简约显示
      NSString *title = ;
      title = ;
      ;
    } else if(number > 0) {   // 一万条以下数据显示实际数值
       forState:UIControlStateNormal];
    } else {                  // 数值为0则显示文字
      ;
    }
}

@end

5、源码下载
**** Hidden Message *****

相关主题链接
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实战开发之仿新浪微博(小龙虾发布版)

大象装冰箱O 发表于 2014-10-19 09:48:53

支持楼主,学到好多东西

panzilong 发表于 2014-11-5 19:00:28


支持楼主,学到好多东西

yipangzi 发表于 2014-12-3 20:12:37

好东西、、、、、、、

hnxyzhw 发表于 2014-12-22 10:19:34

持续关注下载

warmlight 发表于 2014-12-22 17:25:05

前一个demo下下来运行报错 看看这个demo~~

活在梦里丶 发表于 2015-1-10 21:45:39


支持楼主,希望继续更新

虾兵 发表于 2015-1-15 10:35:18

楼主好人,楼主好人,楼主好人

becool7 发表于 2015-1-26 15:01:24

感谢分享~~~

yc2000 发表于 2015-3-1 12:32:15

下载,,,,,,,,,,
页: [1] 2 3
查看完整版本: ios实战开发之仿新浪微博(第九讲:微博功能完善一)