封装 封装 封装 。。。

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

从一个项目中抽取出来的。和大家一起分享 封装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. ROW_NUMBER() OVER函数运用

    语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN) PARTITION BY:相当于数据库中的group by 说明:row_numbe ...

  2. Shell脚本里的双冒号是什么意思

    这个是代码开发风格,其实也就是一个函数名,相当于下划线分割,但改读成包名之后就意义不一样.这个是根据Google的Shell开发规范进行定义的. 参考: https://google.github.i ...

  3. 【XPath Helper:chrome爬虫网页解析工具 Chrome插件】XPath Helper:chrome爬虫网页解析工具 Chrome插件下载_教程_安装 - 开发者插件 - Chrome插件网

    [XPath Helper:chrome爬虫网页解析工具 Chrome插件]XPath Helper:chrome爬虫网页解析工具 Chrome插件下载_教程_安装 - 开发者插件 - Chrome插 ...

  4. 【scrapy】使用方法概要(四)(转)

    [请初学者作为参考,不建议高手看这个浪费时间] 上一篇文章,我们抓取到了一大批代理ip,本篇文章介绍如何实现downloaderMiddleware,达到随即使用代理ip对目标网站进行抓取的. 抓取的 ...

  5. Delphi7下使用FastMM4

    1> 将文件Replacement BorlndMM DLL/Precompiled/for Delphi IDE/Performance/BorlndMM.dll,替换掉Delphi/Bin下 ...

  6. 关于TFS2010 远程无法创建团队项目的若干问题总结

    今天遇到一个TFS的问题,折腾了好几个小时,故将其记录,给有遇到类似问题的朋友一些参考. 1.本文前提:服务器端只安装了TFS2010,本地没有安装Visual Studio 2010,因此不能在服务 ...

  7. Roundcube login via PHP script

    目前正在整合 roundcube 1.0.5 的邮件系统和其他系统,想取消登录过程,发现了这个,先赞一个! 原文地址: http://blog.philippheckel.com/2008/05/16 ...

  8. window消息机制二

    消息机制 windows是一个消息驱动的系统,会有一个总的系统消息的队列,鼠标.键盘等等都会流入到这个队列中,同时会为每个线程维护一个消息队列(注意默认是有GUI调用的线程才有,对于没有GUI或者窗口 ...

  9. org-mode入门教程

    org-mode 入门教程By Z.H. Fu 切问录 www.fuzihao.org org-mode 入门教程 org-mode是Emacs提供的一个强大的编辑模式,可以用于做会议笔记以及制作各种 ...

  10. 特殊汉字“𣸭”引发的对于字符集的思考;mysql字符集;sqlalchemy字符集设置;客户端字符集设置;

    字符集.字符序的概念与联系 在数据的存储上,MySQL提供了不同的字符集支持.而在数据的对比操作上,则提供了不同的字符序支持. MySQL提供了不同级别的设置,包括server级.database级. ...