iOS學習 - tableViewCell(團購)

Model
//
//  YJTg.h
//  01-tableViewCell-團購
//
//  Created by JACKY-MAC on 15-6-29.
//  Copyright (c) 2015年 www.train.com. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface YJTg : NSObject
@property(nonatomic,copy)NSString *icon;
@property(nonatomic,copy)NSString *price;
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *buyCount;

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

+ (NSMutableArray *)tgs;
@end


//
//  YJTg.m
//  01-tableViewCell-團購
//
//  Created by JACKY-MAC on 15-6-29.
//  Copyright (c) 2015年 www.train.com. All rights reserved.
//

#import "YJTg.h"

@implementation YJTg
- (instancetype)initWithDict:(NSDictionary *)dict
{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}
+(instancetype)tgWithDict:(NSDictionary *)dict
{
    return [[self alloc]initWithDict:dict];
}

+ (NSMutableArray *)tgs
{
    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil]];
    
    NSMutableArray *arrayM = [NSMutableArray array];
    for (NSDictionary *dict in array) {
        [arrayM addObject:[self tgWithDict:dict]];
    }
    return arrayM;
}
@end





View


//
//  YJTgCell.h
//  01-tableViewCell-團購
//
//  Created by JACKY-MAC on 15-6-29.
//  Copyright (c) 2015年 www.train.com. All rights reserved.
//

#import <UIKit/UIKit.h>
@class YJTg;
@interface YJTgCell : UITableViewCell
/** 團購的數據模型 */
@property(nonatomic,strong)YJTg *tg;

/** 提供一個類方法,能夠快速建立 Cell */
+ (instancetype)cellWithTableView:(UITableView *)tableView;
@end


//
//  YJTgCell.m
//  01-tableViewCell-團購
//
//  Created by JACKY-MAC on 15-6-29.
//  Copyright (c) 2015年 www.train.com. All rights reserved.
//

#import "YJTgCell.h"
#import "YJTg.h"
@interface YJTgCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;
@end
@implementation YJTgCell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    // 1. 可重用標示符
    static NSString *ID = @"ID";
    
    // 2. tableView查詢可重用Cell
    YJTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 3. 若是沒有可重用cell
    if (cell ==nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"YJTgCell" owner:nil options:nil] lastObject];
    }
    return cell;
}
- (void)setTg:(YJTg *)tg
{
    // setter方法中,第一句要賦值,不然要在其餘方法中使用模型,將沒法訪問到
    _tg = tg;
    
    
    self.titleLabel.text = tg.title;
    self.iconView.image = [UIImage imageNamed:tg.icon];
    self.priceLabel.text = tg.price;
    self.buyCountLabel.text = tg.buyCount;
}


#pragma mark - 模板提供的方法
/**
 初始化方法
 
 使用代碼建立Cell的時候會被調用,若是使用XIB或者Storyboard,此方法不會被調用
 */

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

/**
 從XIB被加載以後,會自動被調用,若是使用純代碼,不會被執行
 */
- (void)awakeFromNib
{

}

/**
 Cell 被選中或者取消選中是都會被調用
 
 若是是自定義Cell控件,全部的子控件都應該添加到contentView中
 */
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}






//
//  YJTgFooterView.h
//  01-tableViewCell-團購
//
//  Created by JACKY-MAC on 15-6-29.
//  Copyright (c) 2015年 www.train.com. All rights reserved.
//

#import <UIKit/UIKit.h>
@class YJTgFooterView;
@protocol YJTgFooterViewDelegate <NSObject>

/** 視圖的下載按鈕被點擊 */
- (void)tgFooterViewDidDownLoadButtonClick:(YJTgFooterView *)footerView;

@end
@interface YJTgFooterView : UIView
@property (nonatomic,weak)id <YJTgFooterViewDelegate>delegate;

+ (instancetype)footerView;

/** 刷新數據結束後,更新頁腳的視圖顯示 */
- (void)endRefresh;

@end




//
//  YJTgFooterView.m
//  01-tableViewCell-團購
//
//  Created by JACKY-MAC on 15-6-29.
//  Copyright (c) 2015年 www.train.com. All rights reserved.
//

#import "YJTgFooterView.h"
@interface YJTgFooterView()
@property (weak, nonatomic) IBOutlet UIButton *loadMoreButton;
@property (weak, nonatomic) IBOutlet UIView *tipsView;

@end

@implementation YJTgFooterView
+ (instancetype)footerView
{
    return [[[NSBundle mainBundle] loadNibNamed:@"YJTgFooterView" owner:nil options:nil] lastObject];
}
- (IBAction)loadMore
{
        //NSLog(@"加載更多");
        // 1. 隱藏按鈕
    self.loadMoreButton.hidden = YES;
    
        // 2. 顯示提示視圖
    self.tipsView.hidden = NO;
    
    // 延時執行命令,多線程GCD
   // 從此關於延時的操做,統一使用dispatch_after
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        // 塊代碼中的代碼會在1.0秒以後執行
        
        // 3. 加載數據(從網絡服務器加載,須要時間...)
        // view是用來顯示數據的,用代理來實現!
        // 代理是用來監聽的,發生某些事情的時候,通知"代理"-》控制器去"工做"->加載數據
        
        // 3.1 判斷代理是否實現了協議方法
        if ([self.delegate respondsToSelector:@selector(tgFooterViewDidDownLoadButtonClick:)])
        {
                [self.delegate tgFooterViewDidDownLoadButtonClick:self];
        }
    
        // 4. 加載完成數據
        self.loadMoreButton.hidden = NO;
        self.tipsView.hidden = YES;
        
    });

}

- (void)endRefresh
{
    
    // 4. 加載完成數據
    self.loadMoreButton.hidden = NO;
    self.tipsView.hidden = YES;
}




//
//  YJTgHeadView.h
//  01-tableViewCell-團購
//
//  Created by JACKY-MAC on 15-6-30.
//  Copyright (c) 2015年 www.train.com. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface YJTgHeadView : UIView
+ (instancetype)hesdView;
@end



//
//  YJTgHeadView.m
//  01-tableViewCell-團購
//
//  Created by JACKY-MAC on 15-6-30.
//  Copyright (c) 2015年 www.train.com. All rights reserved.
//

#import "YJTgHeadView.h"

#define kImageCount 5
@interface YJTgHeadView()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

@property(nonatomic,strong)NSTimer *timer;
@end

@implementation YJTgHeadView

+ (instancetype)hesdView;
{
    return [[[NSBundle mainBundle] loadNibNamed:@"YJTgHeadView" owner:nil options:nil] lastObject];
}
- (void)awakeFromNib
{
    // 1.添加圖片
    CGFloat imageW = self.scrollView.frame.size.width;
    CGFloat imageH = self.scrollView.frame.size.height;
    CGFloat imageY = 0;
    for (int index = 0; index < kImageCount; index++) {
        CGFloat imageX = index * imageW;
        
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"ad_%02d",index]];
        
        [self.scrollView addSubview:imageView];
    }
    // 2.設置內容尺寸
    
  self.scrollView.contentSize = CGSizeMake(imageW * kImageCount, 0);
    
    // 取消水平滾動條
      self.scrollView.showsVerticalScrollIndicator = NO;
      self.scrollView.showsHorizontalScrollIndicator = NO;
    
    // 取消彈簧效果
  self.scrollView.bounces = NO;
    
    // 設置代理
    self.scrollView.delegate = self;
    //是否分頁
    self.scrollView.pagingEnabled = YES;
    
        // 3.分頁
    self.pageControl.numberOfPages = kImageCount;
    
        // 4.定時器
    self.timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
  
}

- (void)nextImage
{
        // 1.下一頁
    if (self.pageControl.currentPage == kImageCount - 1) {
        self.pageControl.currentPage = 0;
    }else{
        self.pageControl.currentPage++;
    }
    
    // 2.設置滾動
    CGPoint offset = CGPointMake(self.scrollView.frame.size.width * self.pageControl.currentPage, 0);
    [self.scrollView setContentOffset:offset animated:YES];
}

#pragma mark - 代理方法
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    self.timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    

}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    [self.timer invalidate];
    self.timer = nil;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.timer) return;
    self.pageControl.currentPage = (scrollView.contentOffset.x + scrollView.frame.size.width * 0.5) / scrollView.frame.size.width;
}
@end




Controller

//
//  YJViewController.m
//  01-tableViewCell-團購
//
//  Created by JACKY-MAC on 15-6-29.
//  Copyright (c) 2015年 www.train.com. All rights reserved.
//

#import "YJViewController.h"
#import "YJTg.h"
#import "YJTgCell.h"
#import "YJTgFooterView.h"
#import "YJTgHeadView.h"

@interface YJViewController ()<YJTgFooterViewDelegate>
@property(nonatomic,strong)NSMutableArray *tgs;
@end

@implementation YJViewController

- (NSArray *)tgs
{
    if (_tgs == nil) {
        _tgs = [YJTg tgs];
    }
    return _tgs;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.rowHeight = 80;
    
    // 調整邊距,能夠讓表格視圖讓開狀態欄;
    self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
    
      // 從XIB加載最後一個視圖設置爲footerView
   // self.tableView.tableFooterView = [[[NSBundle mainBundle] loadNibNamed:@"YJTgFooterView" owner:nil options:nil] lastObject];
    YJTgFooterView *footer = [YJTgFooterView footerView];
    footer.delegate = self;
    self.tableView.tableFooterView = footer;
    
    self.tableView.tableHeaderView = [YJTgHeadView hesdView];
}


#pragma mark - footView的代理方法
- (void)tgFooterViewDidDownLoadButtonClick:(YJTgFooterView *)footerView
{
    // 加載數據
    NSLog(@"努力加載數據中....");
  
    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        // 得到網絡數據以後執行的操做
        
        // 向數組中添加數據,模擬網絡加載完成,以後的效果
        NSDictionary *dict = @{@"title": @"哈哈",@"icon": @"ad_00",@"price": @"100.2",@"buyCount": @"250"};
        YJTg *tg = [YJTg tgWithDict:dict];
        
        [self.tgs addObject:tg];
        
        
        // 刷新數據
        //    [self.tableView reloadData];
        // 新建一個indexPath
        NSIndexPath *path = [NSIndexPath indexPathForRow:(self.tgs.count - 1) inSection:0];
        [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
        

    });
    }
#pragma mark - 數據源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tgs.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 
        // 1. 建立cell
    YJTgCell *cell = [YJTgCell cellWithTableView:tableView];

    // 2> 經過數據模型,設置Cell內容,能夠讓視圖控制器不須要了解cell內部的實現細節
    cell.tg = self.tgs[indexPath.row];

    
    return cell;
}

@end
相關文章
相關標籤/搜索