封装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/ ...
随机推荐
- 理解onPause和onStop
onPause 用于由一个Activity转到另一个Activity.设备进入休眠状态(屏幕锁住了).或者有dialog弹出时 onStop 用于不可见的Activity(有对话框弹出时,这时底下的a ...
- Siege(开源Web压力测试工具)——多线程编程最佳实例
在英语中,"Siege"意为围攻.包围.同时Siege也是一款使用纯C语言编写的开源WEB压测工具,适合在GNU/Linux上运行,并且具有较强的可移植性.之所以说它是多线程编程的 ...
- 撩课-Java每天5道面试题第13天
撩课Java+系统架构点击开始学习 96.JDBC操作数据库的步骤 ? .加载数据库驱动 .创建并获取数据库链接 .创建jdbc statement对象 .设置sql语句 .设置sql语句中的参数(使 ...
- 射洪中学讲课PPT
咸鱼跑去射洪中学讲课去啦. 高中生好萌呀!!!! 下面是讲课PPT day3 简单吹了一波noip2015如何骗分,所以课件就是去网上到处kuai题解而已,所以课件我就不上传了. day4 题目+题解 ...
- UVALive 5971
Problem J Permutation Counting Dexter considers a permutation of first N natural numbers good if it ...
- UVA 11945 Financial Management 水题
Financial Management Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 acm.hust.edu.cn/vjudge/problem/vis ...
- [Visual Studio] SOA服务框架搭建
1.服务框架搭建 2.服务模板创建 3.Nuget引用 4.客户端调用 任务点: 1.分析SOA 2.修改SOA架构名称以及关键字 3.使用Nuget添加引用 4.选择服务模板进行创建 5.尝试调用 ...
- java开发C语言编译器
http://study.163.com/course/introduction.htm?courseId=1003169025 http://study.163.com/course/courseM ...
- python文本 maketrans和translate
python文本 maketrans和translate 场景: 过滤字符串的某些字符,我们从例子出发 >>> tb=str.maketrans ('abc','123') & ...
- andriod 播放mp4
权限 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> activi ...