无限循环滚动大图(UICollectionView)


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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值