zoukankan      html  css  js  c++  java
  • 020606-09-头部尾部

    //  XMGDealsViewController.h
    //  06-自定义等高cell01-storyboard
    #import <UIKit/UIKit.h>
    
    @interface XMGDealsViewController : UITableViewController
    
    @end
    //  XMGDealsViewController.m
    //  06-自定义等高cell01-storyboard
    #import "XMGDealsViewController.h"
    #import "XMGDeal.h"
    #import "XMGDealCell.h"
    #import "XMGPageView.h"
    #import "XMGLoadMoreFooter.h"
    
    @interface XMGDealsViewController ()
    /** 所有的团购数据 */
    @property (nonatomic, strong) NSMutableArray *deals;
    @end
    
    @implementation XMGDealsViewController
    
    - (NSMutableArray *)deals
    {
        if (_deals == nil) {
            // 加载plist中的字典数组
            NSString *path = [[NSBundle mainBundle] pathForResource:@"deals.plist" ofType:nil];
            NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
            
            // 字典数组 -> 模型数组
            NSMutableArray *dealArray = [NSMutableArray array];
            for (NSDictionary *dict in dictArray) {
                XMGDeal *deal = [XMGDeal dealWithDict:dict];
                [dealArray addObject:deal];
            }
            
            _deals = dealArray;
        }
        return _deals;
    }
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];;
        
    //    UIButton *logout = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [logout setTitle:@"退出登录" forState:UIControlStateNormal];
    //    logout.frame = CGRectMake(0, 0, 100, 44);
    //    logout.backgroundColor = [UIColor redColor];
    //    [logout addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
        self.tableView.tableFooterView = [XMGLoadMoreFooter footer];
        
        XMGPageView *pageView = [XMGPageView pageView];
        pageView.imageNames = @[@"2c97690e72365e38e3e2a95b934b8dd2", @"2c97690e72365e38e3e2a95b934b8dd2", @"2c97690e72365e38e3e2a95b934b8dd2", @"2c97690e72365e38e3e2a95b934b8dd2"];
        self.tableView.tableHeaderView = pageView;
        
        // 监听加载更多的通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadMoreDeals) name:@"loadMore" object:nil];
    }
    
    - (void)dealloc
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    - (void)loadMoreDeals {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 加载数据
            XMGDeal *deal = [[XMGDeal alloc] init];
            deal.icon = @"2c97690e72365e38e3e2a95b934b8dd2";
            deal.title = @"xxxx";
            deal.price = @"6546";
            deal.buyCount = @"90";
            [self.deals addObject:deal];
            
            // 刷新表格
            [self.tableView reloadData];
            
            // 结束footer的加载状态
            XMGLoadMoreFooter *footer = (XMGLoadMoreFooter *)self.tableView.tableFooterView;
            [footer endLoading];
        });
    }
    
    #pragma mark - Table view data source
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return self.deals.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        // 创建cell
        XMGDealCell *cell = [XMGDealCell cellWithTableView:tableView];
        
        // 取出模型数据
        cell.deal = self.deals[indexPath.row];
        
        return cell;
    }
    
    @end
    //  XMGLoadMoreFooter.h
    //  06-自定义等高cell01-storyboard
    #import <UIKit/UIKit.h>
    
    @interface XMGLoadMoreFooter : UIView
    + (instancetype)footer;
    
    /**
     * 结束加载状态
     */
    - (void)endLoading;
    @end
    //  XMGLoadMoreFooter.m
    //  06-自定义等高cell01-storyboard
    #import "XMGLoadMoreFooter.h"
    
    @interface XMGLoadMoreFooter()
    @property (weak, nonatomic) IBOutlet UIButton *loadMoreButton;
    @property (weak, nonatomic) IBOutlet UIView *loadingMoreView;
    @end
    
    @implementation XMGLoadMoreFooter
    
    + (instancetype)footer
    {
        return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
    }
    
    /**
     * 点击了加载更多
     */
    - (IBAction)loadMore {
        self.loadMoreButton.hidden = YES;
        self.loadingMoreView.hidden = NO;
        
        // 发出通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"loadMore" object:nil];
    }
    
    /**
     * 结束加载状态
     */
    - (void)endLoading
    {
        self.loadMoreButton.hidden = NO;
        self.loadingMoreView.hidden = YES;
    }
    
    @end
    //  XMGPageView.h
    //  08-分页
    #import <UIKit/UIKit.h>
    
    @interface XMGPageView : UIView
    + (instancetype)pageView;
    /** 图片名字 */
    @property (nonatomic, strong) NSArray *imageNames;
    /** 其他圆点颜色 */
    @property (nonatomic, strong) UIColor *otherColor;
    /** 当前圆点颜色 */
    @property (nonatomic, strong) UIColor *currentColor;
    @end
    //  XMGPageView.m
    //  08-分页
    // 框架:
    // 1.苹果官方的框架:UIKit
    // 2.自定义框架
    // 3.第三方框架:流媒体
    
    #import "XMGPageView.h"
    
    @interface XMGPageView() <UIScrollViewDelegate>
    @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
    @property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
    /** 定时器 */
    @property (nonatomic, strong) NSTimer *timer;
    @end
    
    @implementation XMGPageView
    #pragma mark - 初始化方法
    /**
     * 当控件通过代码创建时,就会调用这个方法
     * 当控件通过代码创建时,想做一些初始化操作。应该在这个方法中执行
     */
    - (instancetype)initWithFrame:(CGRect)frame
    {
        if (self = [super initWithFrame:frame]) {
            [self setup];
            
            // 添加子控件代码
        }
        return self;
    }
    
    /**
     * 当控件从xibstoryboard中创建完毕时,就会调用这个方法
     * 当控件从xibstoryboard中创建完毕后的初始化操作。应该在这个方法中执行
     */
    - (void)awakeFromNib
    {
        [self setup];
    }
    
    /**
     * 初始化代码
     */
    - (void)setup
    {
        self.scrollView.backgroundColor = [UIColor redColor];
        
        // 开启定时器
        [self startTimer];
    }
    
    + (instancetype)pageView
    {
        return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
    }
    
    /**
     * 当控件的尺寸发生改变的时候,会自动调用这个方法
     */
    - (void)layoutSubviews
    {
        [super layoutSubviews];
        
        // 设置scrollView的frame
        self.scrollView.frame = self.bounds;
        
        // 获得scrollview的尺寸
        CGFloat scrollW = self.scrollView.frame.size.width;
        CGFloat scrollH = self.scrollView.frame.size.height;
        
        // 设置pageControl
        CGFloat pageW = 100;
        CGFloat pageH = 20;
        CGFloat pageX = scrollW - pageW;
        CGFloat pageY = scrollH - pageH;
        self.pageControl.frame = CGRectMake(pageX, pageY, pageW, pageH);
        
        // 设置内容大小
        self.scrollView.contentSize = CGSizeMake(self.imageNames.count * scrollW, 0);
        
        // 设置所有imageView的frame
        for (int i = 0; i<self.scrollView.subviews.count; i++) {
            UIImageView *imageView = self.scrollView.subviews[i];
            imageView.frame = CGRectMake(i * scrollW, 0, scrollW, scrollH);
        }
    }
    
    #pragma mark - setter方法的重写
    - (void)setImageNames:(NSArray *)imageNames
    {
        _imageNames = imageNames;
        
        // 移除之前的所有imageView
        // 让subviews数组中的所有对象都执行removeFromSuperview方法
        [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        
        // 根据图片名创建对应个数的imageView
        for (int i = 0; i<imageNames.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.image = [UIImage imageNamed:imageNames[i]];
            [self.scrollView addSubview:imageView];
        }
        
        // 设置总页数
        self.pageControl.numberOfPages = imageNames.count;
    //    if (imageNames.count <= 1) {
    //        self.pageControl.hidden = YES;
    //    } else {
    //        self.pageControl.hidden = NO;
    //    }
    //    self.pageControl.hidden = imageNames.count <= 1;
    //    self.pageControl.hidesForSinglePage = YES;
    }
    
    - (void)setCurrentColor:(UIColor *)currentColor
    {
        _currentColor = currentColor;
        
        self.pageControl.currentPageIndicatorTintColor = currentColor;
    }
    
    - (void)setOtherColor:(UIColor *)otherColor
    {
        _otherColor = otherColor;
        
        self.pageControl.pageIndicatorTintColor = otherColor;
    }
    
    #pragma mark - <UIScrollViewDelegate>
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        self.pageControl.currentPage = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);
    }
    
    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
    {
        [self stopTimer];
    }
    
    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    {
        [self startTimer];
    }
    
    #pragma mark - 定时器控制
    - (void)startTimer
    {
        // 创建一个定时器
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    }
    
    - (void)stopTimer
    {
        [self.timer invalidate];
        self.timer = nil;
    }
    
    /**
     * 下一页
     */
    - (void)nextPage
    {
        NSInteger page = self.pageControl.currentPage + 1;
        if (page == self.pageControl.numberOfPages) {
            page = 0;
        }
        
        CGPoint offset = self.scrollView.contentOffset;
        offset.x = page * self.scrollView.frame.size.width;
        [self.scrollView setContentOffset:offset animated:YES];
        
    //    NSLog(@"nextPage");
    }
    @end
    //  XMGDealCell.h
    //  06-自定义等高cell01-storyboard
    #import <UIKit/UIKit.h>
    @class XMGDeal;
    
    @interface XMGDealCell : UITableViewCell
    /** 团购模型数据 */
    @property (nonatomic, strong) XMGDeal *deal;
    
    + (instancetype)cellWithTableView:(UITableView *)tableView;
    @end
    //  XMGDealCell.m
    //  06-自定义等高cell01-storyboard
    #import "XMGDealCell.h"
    #import "XMGDeal.h"
    
    @interface XMGDealCell()
    @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 XMGDealCell
    
    + (instancetype)cellWithTableView:(UITableView *)tableView
    {
        static NSString *ID = @"deal";
        XMGDealCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([XMGDealCell class]) owner:nil options:nil] lastObject];
            //设置 右边显示箭头
              cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            //设置选中状态
              cell.selectionStyle = UITableViewCellSelectionStyleNone;//(取消)
        }
        return cell;
    }
    
    - (void)setDeal:(XMGDeal *)deal
    {
        _deal = deal;
        
        // 设置数据
        self.iconView.image = [UIImage imageNamed:deal.icon];
        self.titleLabel.text = deal.title;
        self.priceLabel.text = [NSString stringWithFormat:@"¥%@", deal.price];
        self.buyCountLabel.text = [NSString stringWithFormat:@"%@人已购买", deal.buyCount];
    }
    
    @end
    //  XMGDeal.h
    //  06-自定义等高cell01-storyboard
    #import <Foundation/Foundation.h>
    
    @interface XMGDeal : NSObject
    @property (strong, nonatomic) NSString *buyCount;
    @property (strong, nonatomic) NSString *price;
    @property (strong, nonatomic) NSString *title;
    @property (strong, nonatomic) NSString *icon;
    
    + (instancetype)dealWithDict:(NSDictionary *)dict;
    @end
    //  XMGDeal.m
    //  06-自定义等高cell01-storyboard
    #import "XMGDeal.h"
    
    @implementation XMGDeal
    + (instancetype)dealWithDict:(NSDictionary *)dict
    {
        XMGDeal *deal = [[self alloc] init];
        
    //    deal.title = dict[@"title"];
    //    deal.icon = dict[@"icon"];
    //    deal.buyCount = dict[@"buyCount"];
    //    deal.price = dict[@"price"];
        
        // KVC - Key Value Coding
        [deal setValuesForKeysWithDictionary:dict];
        
        return deal;
    }
    @end
    本人无商业用途,仅仅是学习做个笔记,特别鸣谢小马哥,学习了IOS,另日语学习内容有需要文本和音频请关注公众号:riyuxuexishuji
  • 相关阅读:
    iOS的一些面试题分析总结(1)
    iOS的一些面试题分析总结(0)
    iOS页面间传值的一些方式总结
    自定义UIButton
    iOS查看3D效果的手势交互
    关于php得到参数数据
    ios安装ipa与安卓安装apk
    听说程序员想当就能当?
    W5100S、W5500、W5100差异对比
    annot read lifecycle mapping metadata for artifact org.apache.maven.plugins:maven-clean-plugin:maven
  • 原文地址:https://www.cnblogs.com/laugh/p/6509027.html
Copyright © 2011-2022 走看看