The MVC mode uses the xib file to customize collectionCell and xibcollectioncell.
The data comes from douban.com ~ For learning and communication only ~
This example uses the SDWebImage framework to download images from the network.
: Https://github.com/rs/SDWebImage
Implementation Effect and framework:
Xib file: Class is the file associated with it
Code Section:
Modal part
CollectionModal. h
#import <Foundation/Foundation.h>@interface CollectionModal : NSObject@property (nonatomic,retain) NSDictionary *images;@property (nonatomic,copy) NSString *title;@end
CollectionModal. m can be used for debugging without writing anything or rewriting description ~
View
CollectionView. h
# Import <UIKit/UIKit. h> # import "CollectionModal. h "@ interface CollectionCell: UICollectionViewCell // associated with the xib file @ property (weak, nonatomic) IBOutlet UIImageView * movieImage; @ property (weak, nonatomic) IBOutlet UILabel * nameLabel; // data @ 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];} // layout. After the modal value is assigned, call this function-(void) layoutSubviews {[super layoutSubviews]; // do not forget the parent class method, otherwise, it is easy to make a mess of errors _ nameLabel. text = _ modal. title; NSString * str = _ modal. images [@ "medium"]; [_ movieImage sd_setImageWithURL: [NSURL URLWithString: str];} @ end
Controller
CollectionViewController. h
# Import <UIKit/UIKit. h> @ interface CollectionViewController: UIViewController <strong, UICollectionViewDataSource> // do not forget to follow the protocol {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 // file parsing, loading 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]; // load the file into the data array }}# pragma mark-collectionView // create collectionView-(void) _ creatcollecolleview {UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init]; layout. minimumInteritemSpacing = 1; // The distance between internal cells is layout. minimumLineSpacing = 10; // line spacing layout. itemSize = CGSizeMake (Zwidth-4)/3,200); layout. scrollDirection = UICollectionViewScrollDirectionVertical; // set the scroll direction _ collectionView = [[UICollectionView alloc] initWithFrame: CGRectMake (0, 0, Zwidth, Zheight) collectionViewLayout: layout]; // proxy settings _ collectionView. dataSource = self; _ collectionView. delegate = self; [self. view addSubview: _ collectionView]; // register cell UINib * nib = [UINib nibWithNibName: @ "CollectionCell" bundle: [NSBundle mainBundle]; [_ collectionView registerNib: nib accept: @ "cell"];} // implementation of Protocol methods. The following two methods must be implemented:-(NSInteger) collectionView :( UICollectionView *) collectionView numberOfItemsInSection :( NSInteger) section {return _ modalArray. count;}-(UICollectionViewCell *) collectionView :( UICollectionView *) collectionView cellForItemAtIndexPath :( NSIndexPath *) indexPath {// The Reuse idea of cell contains CollectionCell * cell = [collectionView detail: @ "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;}
If you want data, you can comment or send a private message ~ Haha ~
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.