一、添加控件
(1)、添加xib控件
使用约束,控制底部文字text的位置和高度。
(2)、增加需要的数据
这里,我们将图片和文字保存在plist中
(3)、创建该控件的模型
// IWNews.h
#import <Foundation/Foundation.h>
@interface IWNews : NSObject
@property (copy, nonatomic) NSString *title;
@property (copy, nonatomic) NSString *icon;
@end
xib数据绑定
//
// IWNewsCell.m
// 0729-无限滚动
//
// Created by Corwien on 16-8-2.
// Copyright (c) 2016年 Corwien. All rights reserved.
//
#import "IWNewsCell.h"
#import "IWNews.h"
@interface IWNewsCell()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@end
@implementation IWNewsCell
- (void)setNews:(IWNews *)news
{
_news = news;
self.iconView.image = [UIImage imageNamed:news.icon];
self.titleLabel.text = [NSString stringWithFormat:@" %@", news.title];
}
@end
控制器:
//
// IWViewController.m
// 0729-无限滚动
//
// Created by kaiyi on 16-7-30.
// Copyright (c) 2016年 kaiyi. All rights reserved.
//
#import "IWViewController.h"
#import "IWNewsCell.h"
#import "IWNews.h"
#import "MJExtension.h"
@interface IWViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) NSArray *newses;
@end
@implementation IWViewController
-(NSArray *)newses
{
if(!_newses)
{
self.newses = [IWNews objectArrayWithFilename:@"newses.plist"];
}
return _newses;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 注册cell
[self.collectionView registerNib:[UINib nibWithNibName:@"IWNewsCell" bundle:nil] forCellWithReuseIdentifier:@"news"];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 100;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.newses.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"news";
IWNewsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
cell.news = self.newses[indexPath.item];
return cell;
}
@end
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。