COMPANY SERVICE STAFF BLOG NEWS CONTACT

STAFF BLOG

スタッフブログ

TECHNICAL

テクログ

2014.06.02

Objective-C | UICollectionViewを引っ張って更新

テクログ

Mac,iPhone,Objective-C 全て利用歴6ヶ月の新米プログラマーが送る

自分が使って役立ったmethod&Tips集

シリーズ第11弾です。

リストを引っ張ったらくるくるして更新するやつをやってみます。

TeamScheduler のグループ画面にもこっそり導入されております。

UITableViewでやるのが普通なんでしょうが、今回はUICollectionViewでCellの色がランダムで変わるようにします。

一部methodが違うだけで特に違いはありません。

cellForItemAtIndexPath / forRowAtIndexPathの様にUICollectionViewは○○Item、UITableViewは○○Rowです。

事前準備として

新しいフォームにUICollectionViewを置いて、その上にUICollectionViewCellを4つ置きます。

UICollectionViewは高さがあるとスクロール出来ないので、高さをあらかじめ短くしておきます。

UICollectionViewCellのidentifierにcell1~cell4までそれぞれ名前を付けておきます。

reload1

やっとプログラムの説明

ViewController.h

UICollectionView をoutlet接続して、DataSourceとDelegateの使用を宣言してあります

#import 

@interface ViewController : UIViewController<uicollectionviewdatasource, uicollectionviewdelegate="">

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end
</uicollectionviewdatasource,>

ViewController.m


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
BOOL nameflag = YES;

- (void)viewDidLoad
{
    [super viewDidLoad];
?// Do any additional setup after loading the view, typically from a nib.
    
    // initialize
    [_collectionView setDataSource:self];
    [_collectionView setDelegate:self];

    // 引っ張って更新
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc]init];
    // 更新アクションを設定
    [refreshControl addTarget:self action:@selector(pullDown:) forControlEvents:UIControlEventValueChanged];
    
    // UICollectionViewControllerにUIRefreshControlをadd
    [_collectionView addSubview:refreshControl];
    [_collectionView reloadData];

}

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



#pragma mark -collection view
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

// 各セクションの項目数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

//Method to create cell at index path
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    NSInteger row = indexPath.row;
    
    UICollectionViewCell *cell = nil;
    
    NSString *cellname = [NSString stringWithFormat:@"cell%d", row+1];
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellname forIndexPath:indexPath];
    cell.backgroundColor = [self randomColor];

    return cell;
}

#pragma mark -action
-(void) pullDown:(id)sender {
    [sender beginRefreshing];

    [_collectionView reloadData];

    [sender endRefreshing];
}

- (UIColor *)randomColor
{
    static NSArray *selectColor = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        selectColor = @[
                    [UIColor blueColor],
                    [UIColor redColor],
                    [UIColor greenColor],
                    [UIColor yellowColor],
                    [UIColor orangeColor],
                    [UIColor purpleColor],
                    [UIColor lightGrayColor],
                    ];
    });
    
    NSInteger randIdx = arc4random() % selectColor.count;
    return selectColor[randIdx];
}

@end

自前でUICollectionViewをセットしてるので、表示用のコードも自前で用意しますUITableViewと必要な物は同じです。名前がちょっと違うだけ

例では、reloadDataを行うだけですが、画像を新規に読み直すとか並び順変える等使えると思います。

参考にさせていただいたサイトです

■Qiita

iOS – UIRefreshControlで引っ張って更新をする

そのほかのObjective-C関連の記事をお探しならコチラをどうぞ

今回も宣伝しておしまいにします。

弊社から出している拙作スケジュールのアプリです。
スケジュールというかちょっとした予定を入力して、社長、同僚、家族がどこ行った??っていうときに第三者が確認するためのアプリです
是非ダウンロードして感想をお聞かせ下さい。

『Team Scheduler』 概要
▼アプリ価格: 無料
▼対応OS: iOS(iPhone/iPod Touch)、Android
▼カテゴリ: 仕事効率化(App Store)、ビジネス(Google Play)
▼対応言語: 日本語
▼推奨環境:
iOS版: iPhone4以降、iOS6. 0以降
Android版: Android OS 2. 3以降
▼ダウンロード:
App Storeからダウンロード
Google playで手に入れよう

この記事を書いた人

core-corp

入社年

出身地

業務内容

特技・趣味

テクログに関する記事一覧

TOP