博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC模式利用xib文件定制collectionCell
阅读量:5786 次
发布时间:2019-06-18

本文共 4349 字,大约阅读时间需要 14 分钟。


数据来源于豆瓣网~仅供学习交流~


本实例练习用到了SDWebImage框架:实现从网络端下载图片的功能

下载地址:


实现效果及框架:

实现效果及框架


xib文件:Class是与之相联系的文件

xib文件


代码部分:

Modal部分
CollectionModal.h

#import 
@interface CollectionModal : NSObject@property (nonatomic,retain) NSDictionary *images;@property (nonatomic,copy) NSString *title;@end

CollectionModal.m 能够什么都不写,也能够重写description来调试用~


View部分

collectionView.h

#import 
#import "CollectionModal.h"@interface CollectionCell : UICollectionViewCell//与xib文件相关联@property (weak, nonatomic) IBOutlet UIImageView *movieImage;@property (weak, nonatomic) IBOutlet UILabel *nameLabel;//数据@property (nonatomic,retain) CollectionModal *modal;@end

collectionView.m

#import "CollectionCell.h"#import "UIImageView+WebCache.h"@implementation CollectionCell- (void)awakeFromNib {    // Initialization code}- (void)setModal:(CollectionModal *)modal {    _modal = modal;    [self setNeedsLayout];}//布局,当modal赋值以后,调用此函数- (void)layoutSubviews {        [super layoutSubviews];//不要忘了父类方法,不然非常easy出乱七八糟的错误    _nameLabel.text = _modal.title;    NSString *str = _modal.images[@"medium"];    [_movieImage sd_setImageWithURL:[NSURL URLWithString:str]];}@end

Controller部分

collectionViewController.h

#import 
@interface CollectionViewController : UIViewController
//不要忘了遵循协议{ UICollectionView *_collectionView; NSMutableArray *_modalArray;}@end
#import "CollectionViewController.h"#import "CollectionModal.h"#import "CollectionCell.h"#define Zwidth [UIScreen mainScreen].bounds.size.width#define Zheight [UIScreen mainScreen].bounds.size.height@interface CollectionViewController ()@end@implementation CollectionViewController- (void)viewDidLoad {    [super viewDidLoad];    [self _loadData];    [self _creatCollectionView];    // Do any additional setup after loading the view.}#pragma mark - Data//文件解析。载入数据- (void)_loadData {    NSString *fliePath = [[NSBundle mainBundle] pathForResource:@"exercise" ofType:@"json"];    NSData *data = [NSData dataWithContentsOfFile:fliePath];    NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];    //    NSLog(@"%@",dataDic);    _modalArray = [NSMutableArray array];    NSArray *subjects = dataDic[@"subjects"];    for (NSDictionary *dic in subjects) {        CollectionModal *modal = [[CollectionModal alloc] init];        modal.images = dic[@"images"];        modal.title = dic[@"title"];        //        NSLog(@"%@",modal);        [_modalArray addObject:modal];//将文件载入到数据数组中    }}#pragma mark - collectionView//创建collectionView- (void)_creatCollectionView {    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];    layout.minimumInteritemSpacing = 1;//内部cell之间距离    layout.minimumLineSpacing = 10;//行间距    layout.itemSize = CGSizeMake((Zwidth-4)/3, 200);    layout.scrollDirection = UICollectionViewScrollDirectionVertical;//滚动方向设置    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, Zwidth, Zheight) collectionViewLayout:layout];    //代理设置    _collectionView.dataSource = self;    _collectionView.delegate = self;    [self.view addSubview:_collectionView];    //注冊cell    UINib *nib = [UINib nibWithNibName:@"CollectionCell" bundle:[NSBundle mainBundle]];    [_collectionView registerNib:nib forCellWithReuseIdentifier:@"cell"];}//协议方法的实现,以下两个方法是必须实现的- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {    return _modalArray.count;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {    //这里包括cell的复用思想    CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];    CollectionModal *modal = _modalArray[indexPath.row];    cell.modal = modal;    return cell;}

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    CollectionViewController *vc = [[CollectionViewController alloc] init];    self.window.rootViewController = vc;    // Override point for customization after application launch.    return YES;}

关于数据有想要的。能够评论或私信~哈哈~

你可能感兴趣的文章
通用社区登陆组件技术分享(开源)中篇:OAuth 登陆组件流程及组件集成方法...
查看>>
WebADI_配置设定10_设定默认值Attribute Default Value(案例)
查看>>
SQL SERVER 2008取出XML数据
查看>>
STL 算法
查看>>
分享:Backbone.js 样例站点与入门指南
查看>>
图的基本算法
查看>>
《架构之美》摘录三
查看>>
myeclipse6.5上基于JAX-WS开发Webservice(中文示例)
查看>>
HTML基础(一)
查看>>
谈谈冒烟测试
查看>>
boost.circular_buffer简介
查看>>
Database Appliance并非Mini版的Exadata-还原真实的Oracle Unbreakable Database Appliance
查看>>
CORTEX-M3 异常/中断控制(使能和除能)
查看>>
HTTP Status Code (http状态码)
查看>>
网页图片缩放(js)
查看>>
没有设计模式画小人,有趣画板
查看>>
Silverlight 浏览器外运行及更新判断
查看>>
(转)NS2无线网络遗失模型
查看>>
虚拟化架构中小型机构通用虚拟化架构
查看>>
cocoa touch 组件
查看>>