封装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/ ...
随机推荐
- ARC 101 D - Median of Medians
题面在这里! 这种题只能二分答案把qwwq,直接做根本做不了啊... 首先你需要知道如何通过 一个区间<=x的数有多少个 来判断x和这个区间中位数的关系. 很显然当数有至少 [L/2]+1 个( ...
- LOJ.2587.[APIO2018]铁人两项Duathlon(圆方树)
题目链接 LOJ 洛谷P4630 先对这张图建圆方树. 对于S->T这条(些)路径,其对答案的贡献为可能经过的所有点数,那么我们把方点权值设为联通分量的大小,可以直接去求树上路径权值和. 因为两 ...
- 【搜索+DP】codevs1066-引水入城
[题目大意] 一个N行M列的矩形,如上图所示,其中每个格子都代表一座城 市,每座城市都有一个海拔高度.现在要在某些城市建造水利设施.水利设施有两种,分别为蓄水厂和输水站.蓄水厂的功能是利用水泵将湖泊中 ...
- 属性通知之ObservableCollection
单个属性是如何去通知,在上一章已经介绍过了,那么集合如何做到属性通知呢?这里要介绍ObservableCollection<T>,字面意思就是用于观察的集合. msdn上给出的定义是:表示 ...
- Send a WhatsApp Message programatically -- Tasker WhatsTasker
Here is My code snippet: Uri mUri = Uri.parse("smsto:+9876543210"); Intent mIntent = new I ...
- VS中运行HTTP 无法注册URL
参考资料 http://www.java123.net/detail/view-449670.html http://www.cnblogs.com/jiewei915/archive/2010/06 ...
- 使用jquery dataTable
jQuery 的插件 dataTables 是一个优秀的表格插件,提供了针对表格的排序.浏览器分页.服务器分页.筛选.格式化等功能.dataTables 的网站上也提供了大量的演示和详细的文档进行说明 ...
- onWindowFocusChanged重要作用 and Activity生命周期
onWindowFocusChanged重要作用 Activity生命周期中,onStart, onResume, onCreate都不是真正visible的时间点,真正的visible时间点是onW ...
- 【docker】centOS7上部署的mysql和spring boot服务,要求,mysql的时间、java程序服务的时间和宿主机的时间完全保持一致【修改mysql时区,临时和永久】【修改spring boot配置文件时区】【修改docker启动spring boot实例程序时区】
要求:centOS7上部署的mysql和spring boot服务,要求,mysql的时间.java程序服务的时间和宿主机的时间完全保持一致: ============================ ...
- 【c语言】模拟实现库函数的atof函数
// 模拟实现库函数的atof函数 #include <stdio.h> #include <string.h> #include <assert.h> #incl ...