



// YYnews.h #import <Foundation/Foundation.h> @interface YYnews : NSObject @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *icon; @end
有了 MJExtension,轉檔只要一行:[YYnews objectArrayWithFilename:@"newses.plist"]。


// YYcell.h #import <UIKit/UIKit.h> @class YYnews; @interface YYcell : UICollectionViewCell @property (nonatomic, strong) YYnews *news; @end
// YYcell.m
@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
這裡刻意覆寫 setter 而不是寫一個 -(void)setNews: 之外的方法,是為了讓赋值動作與屬性賦值綁在一起——外部只要寫 cell.news = model,界面就自動更新,呼叫端不必記得多呼叫一個方法。
#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];
// 用 xib 註冊 cell
[self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil]
forCellWithReuseIdentifier:YYIDCell];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (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;
}
@end
(1)xib 自定義 cell 的註冊方式不同。純程式碼建立時用:
[self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
而 xib 建立時必須改用:
[self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
兩者不能混用——用錯了,dequeueReusableCellWithReuseIdentifier: 會直接 crash。
(2)別忘了在 xib 裡設定 reuse identifier。建立自定義 cell 後,要在屬性面板把識別符設為 cell,否則取出時找不到對應的樣板。

(3)驗證 cell reuse 是否生效。在 cellForItemAtIndexPath: 裡印出 cell 位址:
NSLog(@"%p,%d", cell, indexPath.item);

滾動多圈之後會發現,無論列表多長,實際上只建立了兩三個 cell 實例,其餘都是回收再利用。這就是 UICollectionView 不必為每個資料項都配置一個視圖的原因。
到這裡功能就完整了,但還有個問題:滑到最後一張就到底了,不會回到第一張。plist 裡只有五筆資料,索引沒有第 5 筆。要營造「無限」的錯覺,常見思路是「大量複製資料,再把初始位置設在中間」。

用一個 for 迴圈把五筆資料重複塞進陣列 200 次,共 1000 筆;啟動時定位到第 500 筆。這樣往前後各還有 500 筆的餘裕,使用者滑很久都不會碰到底。
- (NSMutableArray *)news {
if (_news == nil) {
_news = [NSMutableArray array];
for (int i = 0; i < 200; i++) {
NSArray *array = [YYnews objectArrayWithFilename:@"newses.plist"];
[_news addObjectsFromArray:array];
}
}
return _news;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil]
forCellWithReuseIdentifier:YYIDCell];
// 預設定位到第 0 組的第 500 個項目
[self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:500 inSection:0]
atScrollPosition:UICollectionViewScrollPositionLeft
animated:YES];
}
印出索引驗證:

關鍵 API 是:
[self.collectinView scrollToItemAtIndexPath:<#(NSIndexPath *)#>
atScrollPosition:<#(UICollectionViewScrollPosition)#>
animated:<#(BOOL)#>];
另一種思路是不動 news 陣列,而是讓 collection view 宣稱自己有 100 組、每組 5 筆,總共 500 筆;資料取用時仍從原本的五筆陣列裡拿,靠 indexPath.item 對應。啟動時定位到第 50 組的第 0 筆。
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil]
forCellWithReuseIdentifier:YYIDCell];
// 定位到第 50 組的第 0 個項目——剛好在正中央
[self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:50]
atScrollPosition:UICollectionViewScrollPositionLeft
animated:YES];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 100;
}
- (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;
}
這個版本比較省記憶體——模型陣列始終只有五筆,多出來的只是「宣稱」出來的索引空間。
必須誠實指出:兩種做法都製造了大量無用的索引,只是浪費的程度不同。而且實際專案裡,模型總數不宜開太大——UICollectionViewLayout 在計算時會遍歷所有元素的 frame,模型數量過大時這份負擔會直接反映在效能上,白白佔用資源。
更根本的改進方向是:監聽滾動狀態,在 scrollViewDidEndDecelerating: 回呼裡判斷是否即將越界,若接近邊界就把 contentOffset 拉回中間位置。這樣一筆資料都不用複製,也不需要虛構 section,才是真正的「無限」。
另一個實務上常見的替代方案是替換成 UIScrollView 加三個 page(上一張、當前、下一張),每次滑動結束後把三張往中間輪轉,並即時更新內容——記憶體佔用量恆定,與資料總量無關。