iOS开发UI篇—无限轮播(新闻数据展示)

一、实现效果

        

二、实现步骤

1.前期准备

  (1)导入数据转模型的第三方框架MJExtension

  (2)向项目中添加保存有“新闻”数据的plist文件

    

  (3)导入用到的图片素材

2.步骤和代码

(1)新建一个数据模型

   

  该模型的代码设计如下:    

  YYnews.h文件

 //
// YYnews.h
// 08-无限滚动(新闻数据展示)
// #import <Foundation/Foundation.h> @interface YYnews : NSObject
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *icon;
@end

(2)新建一个继承自UICollectionViewCell的类,用于自定义cell。

  

(3)新建一个xib文件,和自定义的cell做关联

     

  代码设计如下:

   YYcell.h文件

 //
// YYcell.h
// 08-无限滚动(新闻数据展示)
// #import <UIKit/UIKit.h> @class YYnews;
@interface YYcell : UICollectionViewCell
@property(nonatomic,strong)YYnews *news;
@end

YYcell.m文件

 //
// YYcell.m
// 08-无限滚动(新闻数据展示)
// #import "YYcell.h"
#import "YYnews.h" @interface YYcell ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIImageView *imageView; @end
@implementation YYcell -(void)setNews:(YYnews *)news
{
_news=news;
self.label.text=news.title;
self.imageView.image=[UIImage imageNamed:news.icon];
} @end

(4)在主控制器中的代码处理

  YYViewController.m文件

 //
// YYViewController.m
//
//
// Created by apple on 14-8-3.
// Copyright (c) 2014年 yangyong. All rights reserved.
// #import "YYViewController.h"
#import "MJExtension.h"
#import "YYnews.h"
#import "YYcell.h" #define YYIDCell @"cell" @interface YYViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectinView;
@property(nonatomic,strong)NSArray *news;
@end @implementation YYViewController #pragma mark-懒加载
-(NSArray *)news
{
if (_news==nil) {
_news=[YYnews objectArrayWithFilename:@"newses.plist"];
}
return _news;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//注册cell
// [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
[self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell]; } #pragma mark- UICollectionViewDataSource
//一共多少组,默认为1组
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return ;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.news.count;
} -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
YYcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:YYIDCell forIndexPath:indexPath];
cell.news=self.news[indexPath.item];
return cell;
} #pragma mark-UICollectionViewDelegate
@end

3.补充说明

(1)如果collectionCell是以xib的方式自定义的,那么在注册cell的时候,需要使用另外一种方式。
[self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
[self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
(2)在自定义xib的时候,使用collectionViewCell。并设置其标识为cell.
  
(3)打印查看cell的利用情况
  

iOS开发UI篇—无限轮播(新闻数据展示)的更多相关文章

  1. iOS开发UI篇—无限轮播(循环展示)

    iOS开发UI篇—无限轮播(循环展示) 一.简单说明 之前的程序还存在一个问题,那就是不能循环展示,因为plist文件中只有五个数组,因此第一个和最后一个之后就没有了,下面介绍处理这种循环展示问题的小 ...

  2. iOS开发UI篇—无限轮播(循环利用)

    iOS开发UI篇—无限轮播(循环利用) 一.无限轮播  1.简单说明 在开发中常需要对广告或者是一些图片进行自动的轮播,也就是所谓的无限滚动. 在开发的时候,我们通常的做法是使用一个UIScrollV ...

  3. iOS开发UI篇—无限轮播(功能完善)

    iOS开发UI篇—无限轮播(功能完善) 一.自动滚动 添加并设置一个定时器,每个2.0秒,就跳转到下一条. 获取当前正在展示的位置. [self addNSTimer]; } -(void)addNS ...

  4. iOS开发UI篇—UIScrollView控件实现图片轮播

    iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播            二.实现代码 storyboard中布局 代码: #import "YYV ...

  5. 【转】 iOS开发UI篇—UIScrollView控件实现图片轮播

    原文:http://www.cnblogs.com/wendingding/p/3763527.html iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播 ...

  6. iOS开发UI篇—核心动画(UIView封装动画)

    iOS开发UI篇—核心动画(UIView封装动画) 一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画 ...

  7. iOS开发UI篇—iOS开发中三种简单的动画设置

    iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView b ...

  8. iOS开发UI篇—CAlayer(自定义layer)

    iOS开发UI篇—CAlayer(自定义layer) 一.第一种方式 1.简单说明 以前想要在view中画东西,需要自定义view,创建一个类与之关联,让这个类继承自UIView,然后重写它的Draw ...

  9. iOS开发UI篇—UITabBarController简单介绍

    iOS开发UI篇—UITabBarController简单介绍 一.简单介绍 UITabBarController和UINavigationController类似,UITabBarControlle ...

随机推荐

  1. paper 95:《模式识别和机器学习》资源

    Bishop的<模式识别和机器学习>是该领域的经典教材,本文搜罗了有关的教程和读书笔记,供对比学习之用,主要搜索的资源包括CSDN:http://download.csdn.net/sea ...

  2. PAT乙级 1032. 挖掘机技术哪家强(20)

    1032. 挖掘机技术哪家强(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 为了用事实说明挖掘机技术到底 ...

  3. Redis的简介与安装

    1.简介 Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted ...

  4. 【20160924】GOCVHelper 图像处理部分(2)

    //根据轮廓的面积大小进行选择     vector<VP>  selectShapeArea(Mat src,Mat& draw,vector<VP> contour ...

  5. Unity3D中Console控制台的扩展

    Assert Store上有一个Editor Console Pro,功能非常全面,百度也能搜到破解.如果有需要建议使用,不要再造车轮 起初因为自带Console功能太弱,有不少可以提升空间.于是尝试 ...

  6. SQL Server 2008教程和Microsoft® SQL Server® 2008 R2 SP2 - Express Edition下载

    教程 SQL Server 2008 Tutorialhttp://www.quackit.com/sql_server/sql_server_2008/tutorial/ 数据库下载 Microso ...

  7. phpstorm相关设置

    1, phpstorm安装好后在编辑页随便点哪里都能把光标移过去,类似于word的“即点即输”.仔细找了下,终于找出来怎么关闭了: 这一功能在phpstorm中不知道叫啥名,去掉方法是 打开File- ...

  8. Populating Tree Item With Record Group In Oracle Forms

    The below plsql program unit could be used in a WHEN-NEW-FORM-INSTANCE trigger to initially populate ...

  9. PHP操作MySQL的常用函数

    某些情况下(如html中),调用php的变量时,要给变量加{},若要使字符串变量加上引号,则还需要在{}外加引号 如: $sql="select * from admin where use ...

  10. 通过配置的方式Autofac 《第三篇》

    一.基本配置 1.通过配置的方式使用Autofac <?xml version="1.0"?> <configuration> <configSect ...