Model
<pre name="code" class="cpp">@interface MJNews : NSObject
@property (copy, nonatomic) NSString *title;
@property (copy, nonatomic) NSString *icon;
@end
View(cell自定义)
@class MJNews;
@interface MJNewsCell : UICollectionViewCell
@property (nonatomic, strong) MJNews *news;
@end
@interface MJNewsCell()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@end
@implementation MJNewsCell
- (void)setNews:(MJNews *)news
{
_news = news;
self.iconView.image = [UIImage imageNamed:news.icon];
// self.titleLabel.text = news.title;
self.titleLabel.text = [NSString stringWithFormat:@" %@", news.title];
}
@end
Ctrol
@interface MJViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) NSArray *newses;
@end
@implementation MJViewController
- (NSArray *)newses
{
if (!_newses) {
self.newses = [MJNews objectArrayWithFilename:@"newses.plist"];
}
return _newses;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 注册cell
[self.collectionView registerNib:[UINib nibWithNibName:@"MJNewsCell" bundle:nil] forCellWithReuseIdentifier:@"news"];
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:2500 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.newses.count * 1000;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"news";
MJNewsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
cell.news = self.newses[indexPath.item % self.newses.count];
return cell;
}
@end