ios开发-实现无限滚动轮播图

一、添加控件

(1)、添加xib控件
《ios开发-实现无限滚动轮播图》

使用约束,控制底部文字text的位置和高度。

(2)、增加需要的数据
这里,我们将图片和文字保存在plist中

《ios开发-实现无限滚动轮播图》

(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
    原文作者:Corwien
    原文地址: https://segmentfault.com/a/1190000006151195
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞