封装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
封装scrollView 循环滚动,tableViewCell(连载) mvc的更多相关文章
- IOS实现自动循环滚动广告--ScrollView的优化和封装
一.问题分析 在许多App中,我们都会见到循环滚动的视图,比如广告,其实想实现这个功能并不难,用ScrollView就可以轻松完成,但是在制作的过程中还存在几个小问题,如果能够正确的处理好这些小问题, ...
- 【转】Android循环滚动广告条的完美实现,封装方便,平滑过渡,从网络加载图片,点击广告进入对应网址
Android循环滚动广告条的完美实现,封装方便,平滑过渡,从网络加载图片,点击广告进入对应网址 关注finddreams,一起分享,一起进步: http://blog.csdn.net/finddr ...
- UIScrollView实现自动循环滚动广告
实现效果如下: 功能说明: 程序运行,图片自动循环播放,采用定时器实现; 当用户用手势触摸滑动时,定时器的自动播放取消,停止触摸时,自动无限播放; 代码如下 : 采用封装视图,外部进行调用即可: 1. ...
- UIScrollView循环滚动1
现在基本每一个商业APP都会有循环滚动视图,放一些轮播广告之类的,都是放在UIScrollView之上.假如我要实现N张图片的轮播,我借鉴了几个博文,得到两种方法实现: [第一种]:如下图(图片来源于 ...
- ios监听ScrollView/TableView滚动的正确姿势
主要介绍 监测tableView垂直滚动的舒畅姿势 监测scrollView/collectionView横向滚动的正确姿势 1.监测tableView垂直滚动的舒畅姿势 通常我们用KVO或者在scr ...
- 使用UIScrollView 结合 UIImageView 实现图片循环滚动
场景: 在开发工作中,有时我们需要实现一组图片循环滚动的情况.当我们使用 UIScrollView 结合 UIImageView 来实现时,一般 UIImageView 会尽量考虑重用,下面例子是以( ...
- NGUI实现的一套不同大小 Item 的循环滚动代码
测试: 数据 & Item 的 Ctrl : using UnityEngine; public class ScrollViewItemData { public int index; p ...
- UIScrollView 循环滚动,代码超简单
如今非常多应用里面多多少少都用到了循环滚动,要么是图片.要么是view,或者是其它,我总结一下,写了个demo分享给大家. 先看代码之后在讲原理: 1.创建一个空的项目(这个我就不多说了). 2.加入 ...
- APP中常见上下循环滚动通知的简单实现,点击可进入详情
APP中常见上下循环滚动通知的简单实现,点击可进入详情 关注finddreams博客,一起分享一起进步!http://blog.csdn.net/finddreams/article/details/ ...
随机推荐
- hdu 4549 矩阵快速幂
题意: M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a, b, n,你能求出F ...
- Java并发(二十):线程本地变量ThreadLocal
ThreadLocal是一个本地线程副本变量工具类. 主要用于将私有线程和该线程存放的副本对象做一个映射,各个线程之间的变量互不干扰,在高并发场景下,可以实现无状态的调用,特别适用于各个线程依赖不同的 ...
- bzoj 3252: 攻略 -- 长链剖分+贪心
3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MB Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神 ...
- 【51nod-1239&1244】欧拉函数之和&莫比乌斯函数之和 杜教筛
题目链接: 1239:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239 1244:http://www.51nod. ...
- JAVA读取XML,JAVA读取XML文档,JAVA解析XML文档,JAVA与XML,XML文档解析(Document Object Model, DOM)
使用Document Object Model, DOM解析XML文档 也可参考我的新浪博客:http://blog.sina.com.cn/s/blog_43ac5543010190w3.html ...
- js:深入prototype(上:内存分析)
/** * 下面演示了通过原型的创建方式,使用基于原型的创建能够将属性和方法 * 设置为Person专有的,不能通过window来调用. * 原型是javascript中的一个特殊对象,当一个函 ...
- DevExpress Components16.2.6 Source Code 编译
DevExpress 是一个比较有名的界面控件套件,提供了一系列优秀的界面控件.这篇文章将展示如何在拥有源代码的情况下,对 DevExpress 的程序集进行重新编译. 特别提示:重编译后,已安装好的 ...
- .NET:CLR via C# Manifest
An assembly is a collection of one or more files containing type definitions and resource files. One ...
- Selenium2+python自动化49-判断文本(text_to_be_present_in_element)
前言 在做结果判断的时候,经常想判断某个元素中是否存在指定的文本,如登录后判断页面中是账号是否是该用户的用户名. 在前面的登录案例中,写了一个简单的方法,但不是公用的,在EC模块有个方法是可以专门用来 ...
- [runtime] MAObjCRuntime
MAObjCRuntime 源码地址:(引入头文件MARTNSObject.h即可,非arc环境下) http://pan.baidu.com/s/1eQ6776U https://github.co ...