顯示具有 uicollectionview 標籤的文章。 顯示所有文章
顯示具有 uicollectionview 標籤的文章。 顯示所有文章

2014年8月24日 星期日

iOS 開發筆記 - UICollectionView resize after device rotation

最近在把玩 UICollectionView 並著手處理 resize 的時機點,經 StackOverflow - Change UICollectionViewCell size on different device orientations 的討論,純 coding without storyboard 時,需處理兩件事:

  • - (CGSize)collectionView:layout:sizeForItemAtIndexPath:
  • - (void)didRotateFromInterfaceOrientation:

就像在 UITableViewController 時,告訴 ViewController 這個 UITableViewCell 到底有多高,只是在 UICollectionViewCell 時,還能定義有多寬;在 didRotateFromInterfaceOrientation 事件中,則是告訴 UIViewController 更新畫面。

只是單純上述兩者並無法改變已經存在的 UICollectionViewCell ,對於已經存在的,必須強制處理,簡言之,需要再多幾件事:

  • 在 - (void)didRotateFromInterfaceOrientation: 時,請記得重新定義 UICollectionView frame 資訊,並且使用 [yourCollectionView reloadData] 的方式重繪資料
  • 在 - (UICollectionViewCell *)collectionView:cellForItemAtIndexPath: 中,若每一個 UICollectionViewCell 並非有規則的,則需要動態偵測並重新設定 frame 資訊
如此一來,就能達到 auto resize 的效果了 Orz  若沒這麼龜毛,大概用一下 Storyboard 就解掉了 XD

2014年8月23日 星期六

iOS 開發筆記 - Use UICollectionView without Storyboard and XIB


最近才發現這個 Class ,真是乃覺三十里 XD 有可能已經習慣自己刻 UITableViewCell 了 Orz 所以一直沒去學習新技能,這次看到一些特效後,原本以為單純手動安排 View 變化,仔細一看才發現 UICollectionView 啦,就順手筆記一下,此外,坊間多為使用 Storyboard、XIB 的做法。
  1. Create an ViewControler extends UIViewController
  2. Add UICollectionView *collectionView property
  3. Init collectionView with UICollectionViewFlowLayout
  4. Use UICollectionViewDelegate and UICollectionViewDataSource
  5. Implement collectionView:numberOfItemsInSection: and collectionView:cellForItemAtIndexPath:
如此一來,就可以動了 XD

//
// TestCollectionViewController.h:
//
#import <UIKit/UIKit.h>

@interface TestCollectionViewController : UIViewController
@property (nonatomic, strong) UICollectionView *collectionView;
@end


//
// TestCollectionViewController.m:
//
#import "TestCollectionViewController.h"

@interface TestCollectionViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@end

@implementation TestCollectionViewController

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * collectionViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    
        CGFloat comps[3];
        for (int i = 0; i < 3; i++)
            comps[i] = (CGFloat)arc4random_uniform(256)/255.f;
        collectionViewCell.backgroundColor = [UIColor colorWithRed:comps[0] green:comps[1] blue:comps[2] alpha:1.0];
    return collectionViewCell;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(100, 100);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
     
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 300, 300) collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    }
    return _collectionView;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
 
    [self.view addSubview:self.collectionView];
 
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end