2013年2月26日火曜日

UITableViewで「次を表示..」ページングを実装

どうも、俺@家です。

 今日はUITableView(UITableViewControllerでも同じだけど)を使ってデータを一覧表示させたときに、 「次を表示...」というセルを用意してページングを行う方法をφ(..)メモメモ

 UITableViewに表示させるデータ(dataSource)は、
NSMutableArray *datas
に保持しているものとします。
 まずページング用のセルを表示するための準備。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section                                 
{
  // データ件数
  int count = [datas count];

  // 初期
  if (count==0) {
    return 0;

  // _offsetCountは現在の画面に表示中のデータ件数
  // 「次を表示」を出すためにcount+1件だけセル数を返すようにする
  } else if (count==_offsetCount) {
    return count+1;
  }

  // 通常はデータ件数を返す
  return count;
}
これで、保持しているデータ+1件目に「次を表示」のセルを出す事ができます。
 次に実際にセルを作ります。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  // まずデータを持った通常のセル
  if (indexPath.row!=[datas count]) {
    // Cell再利用のためのIdentifier
    static NSString *CellIdentifier = @"NormalCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell==nil) {
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseidentifier:CellIdentifier] autorelease];
    }
    // 処理の必要があれば処理を書く
    return cell;

  // ページング用「次を表示」のセル(index.row==[datas count])
  } else {
    static NSString *CellIdentifier = @"NextCell";
    UITableViewCell *nextcell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nextcell==nil) {
      nextcell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    nextcell.textLabel.text = @"次を表示";
    return nextcell;
  }
}
最後にタップされたときに次のデータを読み込ませます。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  // 選択状態解除
  [tableView deselectRowAtIndexPath:indexPath animated:YES];

  // 通常のセルが押された
  if (indexPath.row!=_offsetCount) {
    // ページ遷移などの処理
    XXViewController *viewController = [[XXViewController alloc] initWithNibName:nil bundle:nil];
    [self.navigationController pushViewController:viewController animated:YES];
    [viewController release];

  // 「次を表示」が押された
  } else {
    // 次を読み込ませるメソッド(サーバと通信するなど)
    [self loadNextData];

    // UITableViewを更新させる
    [tableView reloadData];
  }
}
これでざっとページングの完成です!
 よくある実装として、通常のセルと「次を表示」のセルとでサイズが異なる場合などは少しコツが必要なので、
 それはまた今度書きます。

 以上でぇぇえぇぇす。

2013年2月16日土曜日

UINavigationBarのナビゲーションバーに背景画像を設定する

どうも、俺@休み中です。
最近はずっとiOSアプリ開発に勤しんでいます。 

UINavigationBarを使って、ヘッダー部分(ナビゲーションバー)に画像を設定したい場合。 QiitaのUINavigationBarの背景画像を設定する方法を参考にさせてもらいました。
#import <QuartzCore/QuartzCore.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIViewController *viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc];

    // OSのバージョン取得
    float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];

    // iOS5未満
    if (osVersion < 5.0f) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:1]];
        navBg.layer.zPosition = -FLT_MAX;
        [navigationController.navigationBar insertSubView:imageView atIndex:0];

    // iOS5以上
    } else if (osVersion >= 5.0f) {
        navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"background.png"] forBarMetrics:UIBarMetricsDefault];
    }
}
です。
 iOS5以上から
- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics

というメソッドが加わったため、iOS5未満の方法で同じように実装しても思ったように表示されません。

以上でぇぇぇえぇぇぇす。

2013年1月21日月曜日

Objective-Cでファイルのタイムスタンプや属性を取得

どうも、俺@家です。
21日ですが、明けましておめでとうございます。
今年もよろしくお願いしマウス。チューチュー。

NSFileManagerでとあるディレクトリ以下にあるファイルを一覧で取得し、作成された日付順に並び替えたい場合。
// Cacheディレクトリ

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];


// ファイル一覧(サブディレクトリは除外)
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];


// 属性情報を格納する配列
NSMutableArray *attributes = [NSMutableArray array];


// ファイル配列をループ
for (NSString *file in files) {

  // ファイル属性にファイルパスを追加するためにDictionaryを用意しておく
  NSMutableDictionary *tmpDictionary = [NSMutableDictionary dictionary];

  NSString *filepath = [path stringByAppendingPathComponent:file];
   // ファイル情報(属性)を取得
   // 取得できる情報については csoulsの日記 を参考に!
  NSDictionary *attr = [[NSFileManager defaultManager] attributesOfItemAtPath:filepath error:nil];


  // tmp配列に属性を格納
  [tmpDictionary setDictionary:attr];

  // tmp配列にファイルパスを格納
  [tmpDictionary setObject:filepath forKey:@"FilePath"];

  [attributes addObject:tmpDictionary];
}


// ソートさせる
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:NSFileCreationDate ascending:NO];
NSArray *sortarray = [NSArray arrayWithObject:sortDescriptor];

// 並び替えられたファイル配列
NSArray *resultarray = [attributes sortedArrayUsingDescriptors:sortarray];

[sortDescriptor release];


なぜわざわざtmpDictionaryを用意するか?についてですが、
属性を取得する
  NSDictionary *attr = [[NSFileManager defaultManager] attributesOfItemAtPath:filepath error:nil];
は、NSDictionaryなためファイルパスが追加できないためです。


以上でぇぇぇえぇす。

2012年12月16日日曜日

NSDictionaryの特定のキーでソートしちゃる

どうも、俺@まだ勉強中です。

例えばNSArray(またはNSMutableArray)内にNSDictionaryが複数入っているとして、そのNSArrayをNSDictionaryのとあるキーでソートしたい場合。

NSArray *array = [NSArray arrayWithObjects:@[
        @{@"id":@100, @"name":@"bob"},
        @{@"id":@14,  @"name":@"ken"},
        @{@"id":@531, @"name":@"john"},
        @{@"id":@32,  @"name":@"mike"},
        @{@"id":@65,  @"name":@"kozy"},
        @{@"id":@174, @"name":@"shorn"},
        @{@"id":@7,   @"name":@"scott"},
        @{@"id":@865, @"name":@"mikeal"},
        @{@"id":@31,  @"name":@"fun"}
    ]];
このarrayを格納しているNSDictionaryのキー:idでソートしちゃりたい!場合は、
// NSSortDescriptorを生成して
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"id" ascending:YES];
// 配列に入れておいて
NSArray *sortarray = [NSArray arrayWithObject:sortDescriptor];
// ソートしちゃる!
NSArray *resultarray = [array sortedArrayUsingDescriptors:sortarray];
// 不要なオブジェクトは解放しちゃる
[sortDescriptor release];
これでキー:idの値でソートされます。


以上でぇぇえぇぇぇえぇす。

Objective-Cの高速列挙は順番保証されない

どうも、俺@勉強中です。

Objective-Cの高速列挙(for ... in ...)は配列全体を走査するのにとても便利なんだけど、取得される順番が保証されません。
# NSDictonaryのキーの順番通りに値を取り出したい!とする。
NSDictionary *dictionary = @{
    @"1": @{@"name":@"bob"},
    @"2": @{@"name":@"ken"},
    @"3": @{@"name":@"john"},
    @"4": @{@"name":@"mike"},
    @"5": @{@"name":@"kozy"},
    @"6": @{@"name":@"shorn"},
    @"7": @{@"name":@"scott"},
    @"8": @{@"name":@"mikeal"},
    @"9": @{@"name":@"fun"}
};

for (id key in dictionary) {
    NSLog(@"%@", key); // 1,2,3,...と出てくれない
}
順番通りに取り出したい時困る。
なので回避策としてNSArray#sortedArrayUsingComparatorを使う方法があります。
// 一旦NSDictionaryのキーを取得して
NSArray *keys = [dictionary allKeys];

// sortedArrayUsingComparatorを使ってキーをソート
keys = [keys sortedArrayUsingComparator:^(id o1, id o2) {
    return [o1 compare:o2];
}];

// 確認すると順番通りそ
NSLog(@"%@", keys);
NSMutableArray *array = [NSMutableArray array];

// あとは順番通りに値を取得する
for (id key in keys) {
    [array addObject:[dictionary objectForKey:key]];
}

// 完璧!
NSLog(@"%@", array);
これで最初のNSDictionaryのキー順番通りに値を取得できます

以上でぇぇえぇぇす。