博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发UI篇—无限轮播(新闻数据展示)
阅读量:4692 次
发布时间:2019-06-09

本文共 3146 字,大约阅读时间需要 10 分钟。

一、实现效果

        

二、实现步骤

1.前期准备

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

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

    

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

2.步骤和代码

(1)新建一个数据模型

   

  该模型的代码设计如下:    

  YYnews.h文件

1 // 2 //  YYnews.h 3 //  08-无限滚动(新闻数据展示) 4 // 5  6 #import 
7 8 @interface YYnews : NSObject 9 @property(nonatomic,copy)NSString *title;10 @property(nonatomic,copy)NSString *icon;11 @end

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

  

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

     

  代码设计如下:

   YYcell.h文件

1 // 2 //  YYcell.h 3 //  08-无限滚动(新闻数据展示) 4 // 5  6 #import 
7 8 @class YYnews; 9 @interface YYcell : UICollectionViewCell10 @property(nonatomic,strong)YYnews *news;11 @end

 YYcell.m文件

1 // 2 //  YYcell.m 3 //  08-无限滚动(新闻数据展示) 4 // 5  6 #import "YYcell.h" 7 #import "YYnews.h" 8  9 @interface YYcell ()10 @property (weak, nonatomic) IBOutlet UILabel *label;11 @property (weak, nonatomic) IBOutlet UIImageView *imageView;12 13 @end14 @implementation YYcell15 16 -(void)setNews:(YYnews *)news17 {18     _news=news;19     self.label.text=news.title;20     self.imageView.image=[UIImage imageNamed:news.icon];21 }22 23 @end

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

  YYViewController.m文件

1 // 2 //  YYViewController.m 3 //   4 // 5 //  Created by apple on 14-8-3. 6 //  Copyright (c) 2014年 yangyong. All rights reserved. 7 // 8  9 #import "YYViewController.h"10 #import "MJExtension.h"11 #import "YYnews.h"12 #import "YYcell.h"13 14 #define YYIDCell @"cell"15 16 @interface YYViewController ()
17 @property (weak, nonatomic) IBOutlet UICollectionView *collectinView;18 @property(nonatomic,strong)NSArray *news;19 @end20 21 @implementation YYViewController22 23 #pragma mark-懒加载24 -(NSArray *)news25 {26 if (_news==nil) {27 _news=[YYnews objectArrayWithFilename:@"newses.plist"];28 }29 return _news;30 }31 - (void)viewDidLoad32 {33 [super viewDidLoad];34 //注册cell35 // [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];36 [self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];37 38 }39 40 #pragma mark- UICollectionViewDataSource41 //一共多少组,默认为1组42 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView43 {44 return 1;45 }46 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section47 {48 return self.news.count;49 }50 51 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath52 {53 YYcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:YYIDCell forIndexPath:indexPath];54 cell.news=self.news[indexPath.item];55 return cell;56 }57 58 #pragma mark-UICollectionViewDelegate59 @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的利用情况
  

转载于:https://www.cnblogs.com/zengshuilin/p/5766790.html

你可能感兴趣的文章
数据库MySQL/mariadb知识点——触发器
查看>>
Ubuntu做Tomcat服务:insserv: warning: script 'tomcat' missing LSB tags and overrides
查看>>
Binary Agents
查看>>
入门Webpack,看这篇就够了
查看>>
短信拦截马”黑色产业链与溯源取证研究
查看>>
Mac Xdebug安装时遇到了Zend Engine API 不一致的问题
查看>>
最小公倍数
查看>>
asp.net如何定时执行任务
查看>>
在github上实现页面托管预览功能
查看>>
css选择器
查看>>
prim
查看>>
给陌生人写一封信
查看>>
noip2013花匠
查看>>
[CF]Equalize Them All
查看>>
React Ant design table表单与pagination分页配置
查看>>
重大发现: windows下C++ UI库 UI神器-SOUI(转载)
查看>>
linux 压缩文件的命令总结
查看>>
linux tail 命令详解
查看>>
BZOJ-3207 花神的嘲讽计划Ⅰ
查看>>
BZOJ-1069 [SCOI2007]最大土地面积
查看>>