IOS開發(fā)基礎(chǔ)之網(wǎng)易新聞UICollectionView的使用第3天
由于第3天的UICollectionView 并不實(shí)現(xiàn),我查閱相關(guān)資料,也沒解決,先從本地的plist加載的數(shù)據(jù),不是網(wǎng)絡(luò)的上的數(shù)據(jù)。有一定的參考意義。
父類是UIViewController 通過純代碼添加進(jìn)去的,addSubView 方式添加進(jìn)去的,這跟UITableViewController類似,可以拖控件,指定相應(yīng)的類,但是這種方法并沒有學(xué)會。源碼在我的主頁下面。等回頭這塊知識健全了,再補(bǔ)充回來。
#import "HMImageLoopController.h"
#import "YYCell.h"
#import "YYNews.h"
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
#define MaxSections 100
@interface HMImageLoopController()<UICollectionViewDataSource
,UICollectionViewDelegate
>
@property (nonatomic
, strong
) UICollectionView
*collectionView
;
@property (nonatomic
, strong
) UIPageControl
*pageControl
;
@property (nonatomic
, strong
) NSMutableArray
*newses
;
@property (nonatomic
, strong
) NSTimer
*timer
;
@property (nonatomic
, strong
) NSMutableArray
*news
;
@end
@implementation HMImageLoopController
-(NSArray
*)newses
{if (_newses
== nil
) {NSBundle
*bundle
= [NSBundle mainBundle
];NSString
*path
=[bundle pathForResource
:@"resource.plist" ofType
:nil
];NSArray
*array
= [NSArray arrayWithContentsOfFile
:path
];_newses
=[NSMutableArray array
];for (NSDictionary
*dict
in array
) {[_newses addObject
: [YYNews newsWithDict
:dict
]];}}return _newses
;
}- (void)viewDidLoad
{[super viewDidLoad
];[self.view addSubview
:self.collectionView
];[self.view addSubview
:self.pageControl
];[self.collectionView registerClass
:[YYCell class
] forCellWithReuseIdentifier
:@"Cell"];[self addTimer
];}- (void)addTimer
{_timer
= [NSTimer scheduledTimerWithTimeInterval
:8.0 target
:self selector
:@selector(nextPage
) userInfo
:NULL repeats
:YES
];}
- (void)nextPage
{NSIndexPath
*currentIndexPath
= [[self.collectionView indexPathsForVisibleItems
] lastObject
];NSIndexPath
*currentIndexPathSet
= [NSIndexPath indexPathForItem
:currentIndexPath
.item inSection
:MaxSections
/ 2];[self.collectionView scrollToItemAtIndexPath
:currentIndexPathSet atScrollPosition
:UICollectionViewScrollPositionLeft animated
:NO
];NSInteger nextItem
= currentIndexPathSet
.item
+ 1;NSInteger nextSection
= currentIndexPathSet
.section
;if (nextItem
== self.news
.count
) {nextItem
= 0;nextSection
++;}NSIndexPath
*nextIndexPath
= [NSIndexPath indexPathForItem
:nextItem inSection
:nextSection
];[self.collectionView scrollToItemAtIndexPath
:nextIndexPath atScrollPosition
:UICollectionViewScrollPositionLeft animated
:YES
];
}#pragma mark ----ScrollView 代理方法
-(void)scrollViewDidEndDecelerating
:(UIScrollView
*)scrollView
{[self addTimer
];
}
- (void)scrollViewWillBeginDragging
:(UIScrollView
*)scrollView
{[self.timer invalidate
];self.timer
= nil
;
}-(void)scrollViewDidScroll
:(UIScrollView
*)scrollView
{int page
= (int)(scrollView
.contentOffset
.x
/ scrollView
.frame
.size
.width
+ 0.5) % 5;self.pageControl
.currentPage
= page
;
}#pragma mark ---- 創(chuàng)建集合視圖
- (UICollectionView
*)collectionView
{if (!_collectionView
) {UICollectionViewFlowLayout
*flowLayout
= [[UICollectionViewFlowLayout alloc
] init
];flowLayout
.itemSize
= CGSizeMake(kWidth
, kHeight
);flowLayout
.scrollDirection
= UICollectionViewScrollDirectionHorizontal
;flowLayout
.minimumLineSpacing
= 0;_collectionView
= [[UICollectionView alloc
] initWithFrame
:CGRectMake(0, 0, kWidth
, kHeight
) collectionViewLayout
:flowLayout
];_collectionView
.delegate
= self;_collectionView
.dataSource
= self;_collectionView
.showsHorizontalScrollIndicator
= NO
;_collectionView
.pagingEnabled
= YES
;_collectionView
.backgroundColor
= [UIColor clearColor
];[self.collectionView scrollToItemAtIndexPath
:[NSIndexPath indexPathForItem
:0 inSection
:MaxSections
/ 2] atScrollPosition
:UICollectionViewScrollPositionLeft animated
:YES
];}return _collectionView
;}- (UIPageControl
*)pageControl
{if (!_pageControl
) {UIPageControl
*pageControl
= [[UIPageControl alloc
] init
];pageControl
.center
= CGPointMake(kWidth
/ 2, kHeight
- 100);pageControl
.numberOfPages
= _news
.count
;pageControl
.bounds
= CGRectMake(0, 0, 150, 40);pageControl
.enabled
= NO
;pageControl
.pageIndicatorTintColor
= [UIColor blueColor
];pageControl
.currentPageIndicatorTintColor
= [UIColor redColor
];[self.view addSubview
:pageControl
];_pageControl
= pageControl
;}return _pageControl
;
}
#pragma mark --- 數(shù)據(jù)源
- (NSMutableArray
*)news
{if (!_news
) {NSString
*path
= [[NSBundle mainBundle
] pathForResource
:@"resource.plist" ofType
:nil
];NSArray
*arr
= [NSArray arrayWithContentsOfFile
:path
];_news
= [NSMutableArray array
];for (NSDictionary
*dic1
in arr
) {[_news addObject
:[YYNews newsWithDict
:dic1
]];}}return _news
;
}
#pragma mark --- 實(shí)現(xiàn)collectionView代理方法
- (NSInteger
)numberOfSectionsInCollectionView
:(UICollectionView
*)collectionView
{return MaxSections
;
}
- (NSInteger
)collectionView
:(UICollectionView
*)collectionView numberOfItemsInSection
:(NSInteger
)section
{return self.news
.count
;}- (UICollectionViewCell
*)collectionView
:(UICollectionView
*)collectionView cellForItemAtIndexPath
:(NSIndexPath
*)indexPath
{static NSString
*string
= @"Cell";YYCell
*cell
= [collectionView dequeueReusableCellWithReuseIdentifier
:string forIndexPath
:indexPath
];if (!cell
) {cell
= [[YYCell alloc
] init
];}cell
.news
= self.news
[indexPath
.row
];return cell
;
}
@end
#import "YYCell.h"
#import "YYNews.h"@interface YYCell()
@property (weak
, nonatomic
) UILabel
*label
;
@property (weak
, nonatomic
) UIImageView
*imageView
;
@end
@implementation YYCell
-(instancetype
)initWithFrame
:(CGRect
)frame
{self =[super initWithFrame
:frame
];if (self) {UIImageView
*img
= [[UIImageView alloc
] init
];[self.contentView addSubview
:img
];self.imageView
= img
;UILabel
*lab
= [[UILabel alloc
] init
];[self.contentView addSubview
:lab
];self.label
= lab
;}return self;
}-(void)setNews
:(YYNews
*)news
{_news
=news
;[self settingData
];[self settingFrame
];
}#pragma mark 給子控件賦值
-(void) settingData
{self.imageView
.image
= [UIImage imageNamed
:_news
.icon
];self.label
.text
= _news
.title
;}#pragma mark 設(shè)置子控件的frame
-(void) settingFrame
{CGFloat screenWidth
= self.contentView
.frame
.size
.width
;self.imageView
.frame
= CGRectMake(0, 0, screenWidth
, 200);self.label
.frame
= CGRectMake(0, 0, screenWidth
, 200);self.label
.font
= [UIFont systemFontOfSize
:30];self.label
.textAlignment
= NSTextAlignmentCenter
;
}@end
#import <UIKit/UIKit.h>
@class YYNews
;@interface YYCell
: UICollectionViewCell
@property (nonatomic
, strong
) YYNews
*news
;
@end
#import <Foundation/Foundation.h>@interface YYNews
: NSObject
@property (nonatomic
, copy
) NSString
*title
;
@property (nonatomic
, copy
) NSString
*icon
;-(id
)initWithDict
:(NSDictionary
*)dict
;
+(id
)newsWithDict
: (NSDictionary
*) dict
;
@end
#import "YYNews.h"@implementation YYNews
-(id
)initWithDict
:(NSDictionary
*)dict
{if (self=[super init
]) {self.title
= dict
[@"title"];self.icon
= dict
[@"icon"];}return self;
}+(id
)newsWithDict
:(NSDictionary
*)dict
{return [[self alloc
] initWithDict
:dict
];
}
@end
總結(jié)
以上是生活随笔為你收集整理的IOS开发基础之网易新闻UICollectionView的使用第3天的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。