UITableViewでセクションヘッダをタップするとそのセクションの先頭にスクロールさせるには

iPhone, iPad で長いリストを扱うときにセクションの固まり項目をきれいに表示させたくなります。 Newstrush というニュースリーダーアプリでは、記事のカテゴリ毎にセクション化しています。 ↓ このアプリではセクションのヘッダ部分をタップすると、そこが先頭にくるように細工をしてあります。 実装方法は下記の通り。 セクションヘッダビューの定義 セクションヘッダ用のカスタムViewを作成します。 カスタム init メソッドでセクションインデックスとテーブルビューのポインタを渡して、touchesEnded イベントでUITableViewの scrollRectToVisible メソッドにセクションから始まる画面領域の矩形を渡して呼び出します。 TableSectionHeaderView.h #import <UIKit/UIKit.h> @interface TableSectionHeaderView : UIView { @private id _tableView; int _sectionIndex; } \- (id)initWithSectionIndex:(NSUInteger)index forTable:(id)tableView; @end TableSectionHeaderView.m #import "TableSectionHeaderView.h" @implementation TableSectionHeaderView \- (id)initWithSectionIndex:(NSUInteger)index forTable:(id)tableView { self = \[super init\]; if (self) { _sectionIndex = index; _tableView = tableView; } return self; } \- (void)dealloc { _tableView = nil; \[super dealloc\]; } \- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { CGRect rect_ = \[\_tableView rectForSection:\_sectionIndex\]; rect_.size.height = \[_tableView frame\].size.height; \[\_tableView scrollRectToVisible:rect\_ animated:YES\]; } @end ※ UITableView の参照は相互で retain しないように id にしてあります。 ...

2011年7月9日 · Toshimitsu Takahashi

iPhoneアプリで左にアイコンのあるUITabBarもどきを作るには

トレンドトピックというGoogleトレンドとTwitterトレンドのリスト、そこからニュース, ブログ, ツイートの検索結果を表示するアプリをリリースしてます。 バージョン 1.8からTwitterトレンドを追加するにあたり、既存のGoogleトレンドのリストとどう切り替えUIを用意するか考えて、Tab Bar を使うことにしました。 ただ、通常の Tab Bar はテキストがアイコンの下にくるため、49ピクセル使ってしまいます。今回の切り替えでは2項目しかないため、左にアイコンを右にテキストを置くようにしたいと単純に考えました(iPad版では画面に余裕があるため普通に使っています)。 結果から言うと UITabBar を継承してどうにかするのは非常に難しいことがわかりました。layoutSubviewsメソッドでアイコンの位置を変えても、項目がタップされる度に元に戻ってしまいます。 そこでもうUIButtonを2つ並べてイメージをセットして Tab Bar を実現することに。 イメージの作成 Tab Barのアイコンに対してエフェクトをどうかけるのか調べるのは大変なので、そういう状態の静止画を撮れるサンプリングプロジェクトを作りました。 CustomTabBar.h #import <Foundation/Foundation.h> @interface CustomTabBar : UITabBar { } - (void)shiftPosition; @end CustomTabBar.m #import "CustomTabBar.h" @implementation CustomTabBar - (void)shiftPosition { for (UIView *item inself.subviews) { for (UIView *view_ in item.subviews) { NSString *className = NSStringFromClass(\[view_ class\]); if (\[className isEqualToString:@"UITabBarSwappableImageView"\]) { CGRect frame_ = view_.frame; frame_.origin.x = 0; frame_.origin.y += 5; view_.frame = frame_; } } } } @end 上記の CustomTabBar を使った UITabBar に項目をセットした状態にします。 実行時にボタンを押すと shiftPosition メソッドが呼ばれるようにしておき、その都度でスクリーンショットをとります。UITabBarSwappableImageView というドキュメント非公開のクラスがアイコンの ImageView クラスになります。 あとはスクリーンショットから画像を切り出して、通常時と選択時のpngファイルを作ります。これをノーマルとRetina向けにそれぞれ行います。 ...

2011年7月7日 · Toshimitsu Takahashi

Settings.bundle の Root.plist から Title, Value を持つ Dictionary の配列をロケールに合わせて取得する

Settings.bundle の Root.plist をアプリケーションに作成すると、iPhone/iPad の「設定」画面とそれによるパラメータ設定が可能になりますが、その中の PSGroupSpecifier の候補リストをアプリケーション内部から取得する方法について明記します。 設定値は、iOS のシステムロケールによって適切な Title に置き換わります。そこも同様になるように処理します。 Settings.bundle の Root.plist 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>StringsTable</key> <string>Root</string> <key>PreferenceSpecifiers</key> <array> <dict> <key>Type</key> <string>PSGroupSpecifier</string> <key>Title</key> <string>SearchTarget</string> </dict> <dict> <key>Values</key> <array> <string></string> <string>ja_JP</string> <string>en_US</string> <string>en_GB</string> </array> <key>Titles</key> <array> <string>SystemLocale</string> <string>Japan</string> <string>USA</string> <string>UK</string> </array> </dict> </array> </dict> </plist> PreferenceSpecifiers / PSGroupSpecifier の取得処理 Settings.bundleの取得 まず、Settings.bundle への参照を取得します。 ...

2010年9月8日 · Toshimitsu Takahashi