zoukankan      html  css  js  c++  java
  • 封装scrollView 循环滚动,tableViewCell(连载) mvc

    封装 封装 封装 。。。

    封装的重要性太重要了 给大家在送点干货

    从一个项目中抽取出来的。和大家一起分享 封装scrollView 循环滚动。tableViewCell(连载) 

    明天还会更新 tableView 的封装 

    使用了mvc 设计模式

    代码例如以下:

    //
    //  GPMainController.m
    
    
    #import "GPMainController.h"
    #import "GPAdsView.h"
    #import "GPSubViewCell.h"
    #import "GPSubject.h"
    
    @interface GPMainController ()<GPAdsViewDelegate,UITableViewDataSource,UITableViewDelegate>
    @property (weak, nonatomic) IBOutlet UITableView *tableView;
    
    @property(nonatomic,strong)NSArray *plist;
    @property(nonatomic,strong)NSArray *subjects;
    
    
    @end
    
    @implementation GPMainController
    
    
    /**
     *  懒载入
     */
    -(NSArray *)plist
    {
        if (_plist == nil) {
            //路径
            NSString *path = [[NSBundle mainBundle]pathForResource:@"quanquan.plist" ofType:nil];
            
            NSArray *arrays = [NSArray arrayWithContentsOfFile:path];
            
            _plist = arrays;
            NSLog(@"%@",_plist);
        }
        return _plist;
    }
    
    -(NSArray *)subjects
    {
        if (_subjects == nil) {
            NSMutableArray *marray = [[NSMutableArray alloc]init];
            NSArray *dicts = self.plist[1];
            for(NSDictionary *dic in dicts)
            {
                GPSubject *sub = [GPSubject SubjectWithDict:dic];
                [marray addObject:sub];
                
            }
            //NSLog(@"%@",marray);
            _subjects = marray;
        }
        NSLog(@"%@",self.plist);
        return _subjects;
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
    #warning 新增代码
        GPAdsView *adsView = [GPAdsView adsView];
        [self.view addSubview:adsView];
        adsView.images = @[@"ad_00",@"ad_01",@"ad_02",@"ad_03",@"ad_04"];
        //adsView.delegate = self;
        [adsView setAdsViewDidSelectedBlock:^(GPAdsView * adsView, NSString *image, NSInteger index) {
            [self adsViewDidSelected:adsView andImage:image andIndex:index];
        }];
        
        self.tableView.tableHeaderView = adsView;
        self.tableView.rowHeight = 120.f;
       
        
    }
    
    #pragma mark GPAdsViewDelegate 代理方法实现
    -(void)adsViewDidSelected:(GPAdsView *)adsView andImage:(NSString *)image andIndex:(NSInteger)index
    {
        NSLog(@"图片名称:%@,索引值 是%ld",image,index);
    }
    //隐藏状态栏
    -(BOOL)prefersStatusBarHidden
    {
        return YES;
    }
    
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.subjects.count;
    }
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        GPSubject *subject =  self.subjects[indexPath.row];
        
        GPSubViewCell *cell = [GPSubViewCell subjectCellWithTabelView:tableView];
        cell.noteLabel.text = subject.note;
        cell.titleLabel.text = subject.title;
        cell.cardNumberLabel.text = subject.cardNumber;
        cell.iconImageView.image = [UIImage imageNamed:subject.icon];
        //cell.subject = self.subjects[indexPath.row];
        return cell;
    }
    @end
    

    //
    //  GPMainController.h
    
    #import <UIKit/UIKit.h>
    
    @interface GPMainController : UIViewController
    
    @end
    

    //
    //  GPSubViewCell.h
    
    
    #import <UIKit/UIKit.h>
    #import "GPSubject.h"
    
    @interface GPSubViewCell : UITableViewCell
    @property(nonatomic,strong)GPSubject *subject;
    @property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
    @property (weak, nonatomic) IBOutlet UILabel *titleLabel;
    @property (weak, nonatomic) IBOutlet UILabel *cardNumberLabel;
    @property (weak, nonatomic) IBOutlet UILabel *noteLabel;
    
    
    +(id)subViewCell;
    +(id)subjectCellWithTabelView:(UITableView *)tableView;
    @end
    

    //
    //  GPSubViewCell.m
    
    
    #import "GPSubViewCell.h"
    
    @interface GPSubViewCell()
    
    
    @end
    
    @implementation GPSubViewCell
    +(id)subViewCell
    {
        //return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil]lastObject];
        UINib *nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
        return [[nib instantiateWithOwner:nil options:nil]lastObject];
       
    }
    
    +(id)subjectCellWithTabelView:(UITableView *)tableView
    {
       /* static NSString *cellName = @"cell";
        GPSubViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
        if (cell == nil) {
            cell = [GPSubViewCell subViewCell];
        }
        return cell;
        */
        NSString * Identifier = NSStringFromClass([self class]);
        UINib *nib = [UINib nibWithNibName:Identifier bundle:nil];
        
        [tableView registerNib:nib forCellReuseIdentifier:Identifier];
        return [tableView dequeueReusableCellWithIdentifier:Identifier];
    }
    
    
    -(void)setSubject:(GPSubject *)subject
    {
        _subject = subject;
        //更新子控件的数据
        
        self.noteLabel.text = subject.note;
        self.cardNumberLabel.text = subject.cardNumber;
        self.titleLabel.text = subject.title;
        self.iconImageView.image = [UIImage imageNamed:subject.icon];
    }
    @end

    //
    //  GPSubject.h
    
    #import <Foundation/Foundation.h>
    
    @interface GPSubject : NSObject
    
    @property(nonatomic,copy)NSString *title;
    
    @property(nonatomic,copy)NSString *cardNumber;
    
    @property(nonatomic,copy)NSString *note;
    
    @property(nonatomic,copy)NSString *icon;
    
    
    +(id)SubjectWithDict:(NSDictionary *)dict;
    -(id)initWithDict:(NSDictionary *)dict;
    @end
    

    //
    //  GPSubject.m
    
    
    #import "GPSubject.h"
    
    @implementation GPSubject
    +(id)SubjectWithDict:(NSDictionary *)dict
    {
        return [[self alloc]initWithDict:dict];
    }
    -(id)initWithDict:(NSDictionary *)dict
    {
        if (self=[super init]) {//注意!
            //把字典中的数据取出来。存储到模型的属性中
            //1.直接取字典中相应的属性进行赋值
            //缺点:假设属性非常多的话,须要写非常多的赋值语句
            /*
             self.title = dict[@"title"];
             self.note = dict[@"note"];
             self.cardNumber = dict[@"cardNumber"];
             self.icon = dict[@"icon"];
             
             //2.使用setValue forKey 反射机制实现(runtime)写框架一定会用到反射
             [self setValue:dict[@"title"] forKey:@"title"];
             [self setValue:dict[@"note"] forKey:@"icon"];
             [self setValue:dict[@"cardNumber"] forKey:@"cardNumber"];
             [self setValue:dict[@"icon"] forKey:@"icon"];
             
             //必须保证,字典中的key 与模型中的属性--相应
             NSArray *keys = [dict allKeys];
             for(NSString *key in keys)
             {
             [self setValue:[dict objectForKey:key] forKey:key];
             }
             */
            //
            [self setValuesForKeysWithDictionary:dict];
        }
        return self;
    }
    -(NSString *)description
    {
        return [NSString stringWithFormat:@"title = %@,icon = %@",_title,_icon];
    }
    
    @end
    


  • 相关阅读:
    ecshop学习入门
    php跳转页面代码
    PHP验证登录用户名和密码
    使用self关键字调用类体中的静态成员
    PHP中读取文件的几个方法
    MVC5 Entity Framework学习之更新相关数据
    global.asax文件的应用
    DbSet<TEntity> 类
    IDisposable接口详解
    bzoj 3240: [Noi2013]矩阵游戏 矩阵乘法+十进制快速幂+常数优化
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/7346637.html
Copyright © 2011-2022 走看看