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未満の方法で同じように実装しても思ったように表示されません。

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