封装 封装 封装 。。。

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

从一个项目中抽取出来的。和大家一起分享 封装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

封装scrollView 循环滚动,tableViewCell(连载) mvc的更多相关文章

  1. IOS实现自动循环滚动广告--ScrollView的优化和封装

    一.问题分析 在许多App中,我们都会见到循环滚动的视图,比如广告,其实想实现这个功能并不难,用ScrollView就可以轻松完成,但是在制作的过程中还存在几个小问题,如果能够正确的处理好这些小问题, ...

  2. 【转】Android循环滚动广告条的完美实现,封装方便,平滑过渡,从网络加载图片,点击广告进入对应网址

    Android循环滚动广告条的完美实现,封装方便,平滑过渡,从网络加载图片,点击广告进入对应网址 关注finddreams,一起分享,一起进步: http://blog.csdn.net/finddr ...

  3. UIScrollView实现自动循环滚动广告

    实现效果如下: 功能说明: 程序运行,图片自动循环播放,采用定时器实现; 当用户用手势触摸滑动时,定时器的自动播放取消,停止触摸时,自动无限播放; 代码如下 : 采用封装视图,外部进行调用即可: 1. ...

  4. UIScrollView循环滚动1

    现在基本每一个商业APP都会有循环滚动视图,放一些轮播广告之类的,都是放在UIScrollView之上.假如我要实现N张图片的轮播,我借鉴了几个博文,得到两种方法实现: [第一种]:如下图(图片来源于 ...

  5. ios监听ScrollView/TableView滚动的正确姿势

    主要介绍 监测tableView垂直滚动的舒畅姿势 监测scrollView/collectionView横向滚动的正确姿势 1.监测tableView垂直滚动的舒畅姿势 通常我们用KVO或者在scr ...

  6. 使用UIScrollView 结合 UIImageView 实现图片循环滚动

    场景: 在开发工作中,有时我们需要实现一组图片循环滚动的情况.当我们使用 UIScrollView 结合 UIImageView 来实现时,一般 UIImageView 会尽量考虑重用,下面例子是以( ...

  7. NGUI实现的一套不同大小 Item 的循环滚动代码

    测试: 数据 & Item  的 Ctrl : using UnityEngine; public class ScrollViewItemData { public int index; p ...

  8. UIScrollView 循环滚动,代码超简单

    如今非常多应用里面多多少少都用到了循环滚动,要么是图片.要么是view,或者是其它,我总结一下,写了个demo分享给大家. 先看代码之后在讲原理: 1.创建一个空的项目(这个我就不多说了). 2.加入 ...

  9. APP中常见上下循环滚动通知的简单实现,点击可进入详情

    APP中常见上下循环滚动通知的简单实现,点击可进入详情 关注finddreams博客,一起分享一起进步!http://blog.csdn.net/finddreams/article/details/ ...

随机推荐

  1. bzoj 4627: [BeiJing2016]回转寿司 -- 权值线段树

    4627: [BeiJing2016]回转寿司 Time Limit: 10 Sec  Memory Limit: 256 MB Description 酷爱日料的小Z经常光顾学校东门外的回转寿司店. ...

  2. Codeforces Round #353 (Div. 2) A. Infinite Sequence 水题

    A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/675/problem/A Description Vasya likes e ...

  3. Codeforces Round #288 (Div. 2) C. Anya and Ghosts 模拟 贪心

    C. Anya and Ghosts time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. C#高级编程9-第4章 继承

    继承是面向对象的一大特征.要深刻学习继承,需要学会使用调试的技巧来学习它,因为它比较抽象. 继承 继承是指一个具体的类型直接使用另一类型的某些数据成员或函数成员,继承的类是基类(父类),被继承的类是派 ...

  5. Oracle | 给表和字段添加注释

    comment  on  column  表名.字段名   is  '注释内容'; comment on column OPERATOR_INFO.MAIN_OPER_ID is '归属操作员'; c ...

  6. 【scrapy】使用方法概要(一)(转)

    [请初学者作为参考,不建议高手看这个浪费时间] 工作中经常会有这种需求,需要抓取互联网上的数据.笔者就经常遇到这种需求,一般情况下会临时写个抓取程序,但是每次遇到这种需求的时候,都几乎要重头写,特别是 ...

  7. Install WordPress Plugins without FTP Access

    WordPress will only prompt you for your FTP connection information while trying to install plugins o ...

  8. 正确理解java编译时,运行时以及构建时这三个概念

    Java中的许多对象(一般都是具有父子类关系的父类对象)在运行时都会出现两种类型:编译时类型和运行时类型,例如:Person person = new Student();这行代码将会生成一个pers ...

  9. 浅用block 转

    block是一门有用的大后期学问.现在我只是列出一点基本用法. 1.快速枚举(Enumeration) 通常是和NSArray, NSDictionary, NSSet, NSIndexSet放在一起 ...

  10. SQL游标、函数的使用方法

         游标的的使用有日常的开发和维护的过程不使用的并不多,但是碰到一些棘手的问题的时候,游标时常是个非常好的帮手,下面就说下游标的使用方法,方法自己以后查阅,和加深一些印象,下面以一个存储过程为例 ...