iOSアプリのUIを爆速で激ヤバにする2つのライブラリ生産性ガチアゲなオープンソースiOSライブラリ(2)(2/4 ページ)

» 2013年05月15日 18時00分 公開
[川崎順平, 平川裕多,ソニックス]

【3】ソースコードの変更

 まずは、AppDelegateを変更します。EGOSampleViewControllerが表示されるよう、下記の通り、コードを変更します。

  1. #import "AppDelegate.h"
  2. #import "EGOSampleViewController.h"
  3. @implementation AppDelegate
  4. - (BOOL)application:(UIApplication *)application
  5. didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  6. {
  7. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  8. self.window.rootViewController = [[EGOSampleViewController alloc] init];
  9. [self.window makeKeyAndVisible];
  10. return YES;
  11. }
AppDelegate.m

 次に、ヘッダファイルの変更です。EGOSampleViewControllerのヘッダにEGORefreshTableHeaderViewをimportし、必要なDelegateと変数を下記のように記述します。

  1. #import <UIKit/UIKit.h>
  2. #import "EGORefreshTableHeaderView.h"
  3. @interface EGOSampleViewController : UITableViewController
  4. <
  5. EGORefreshTableHeaderDelegate,
  6. UITableViewDelegate,
  7. UITableViewDataSource
  8. >
  9. {
  10. // 更新中を表示するViwe
  11. EGORefreshTableHeaderView *_refreshHeaderView;
  12. BOOL _reloading;
  13. // Cellの文字列、更新時に変更
  14. NSString *_cell_string;
  15. }
  16. @end
EGOSampleViewController.h

 最後に、PullRefreshを行うテーブルと実行時の処理を追加します。EGOSampleViewController.mではEGOTableViewPullRefreshに必要なメソッド実装、テーブル内容表示、更新処理を記述します。今回のサンプルでは、下記のように記述します。

  1. #import "EGOSampleViewController.h"
  2. @implementation EGOSampleViewController
  3. - (void)viewDidLoad {
  4. [super viewDidLoad];
  5. _cell_string = @"Before";
  6. if (_refreshHeaderView == nil) {
  7. // 更新ビューのサイズとデリゲートを指定する
  8. EGORefreshTableHeaderView *view =
  9. [[EGORefreshTableHeaderView alloc] initWithFrame:
  10. CGRectMake(
  11. 0.0f,
  12. 0.0f - self.tableView.bounds.size.height,
  13. self.view.frame.size.width,
  14. self.tableView.bounds.size.height
  15. )];
  16. view.delegate = self;
  17. [self.tableView addSubview:view];
  18. _refreshHeaderView = view;
  19. }
  20. // 最終更新日付を記録
  21. [_refreshHeaderView refreshLastUpdatedDate];
  22. }
  23. // セルの数
  24. - (NSInteger)tableView:(UITableView *)tableView
  25. numberOfRowsInSection:(NSInteger)section {
  26. return 10;
  27. }
  28. // テーブルのセル表示処理
  29. - (UITableViewCell *)tableView:(UITableView *)tableView
  30. cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  31. static NSString *CellIdentifier = @"Cell";
  32. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  33. if (cell == nil) {
  34. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  35. }
  36. // 表示されるセルのテキストを設定
  37. cell.textLabel.text = [NSString stringWithFormat:@"%@ - %d", _cell_string, indexPath.row];
  38. // Update後だったら文字色を変更
  39. if ([_cell_string isEqualToString:@"Update"]) {
  40. cell.textLabel.textColor = [UIColor brownColor];
  41. }
  42. return cell;
  43. }
  44. // スクロールされたことをライブラリに伝える
  45. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  46. [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
  47. }
  48. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
  49. willDecelerate:(BOOL)decelerate {
  50. [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
  51. }
  52. // テーブルを下に引っ張ったら、ここが呼ばれる。テーブルデータをリロードして3秒後にdoneLoadingTableViewDataを呼んでいる
  53. - (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
  54. _reloading = YES;
  55. // 非同期処理
  56. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  57. [queue addOperationWithBlock:^{
  58. // 更新処理など重い処理を書く
  59. // 今回は3秒待ち、_cell_stringをUpdateに変更
  60. [NSThread sleepForTimeInterval:3];
  61. _cell_string = @"Update";
  62. [self.tableView reloadData];
  63. // メインスレッドで更新完了処理
  64. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  65. [self doneLoadingTableViewData];
  66. }];
  67. }];
  68. }
  69. // 更新終了
  70. - (void)doneLoadingTableViewData{
  71. // 更新終了をライブラリに通知
  72. _reloading = NO;
  73. [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
  74. }
  75. // 更新状態を返す
  76. - (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
  77. return _reloading;
  78. }
  79. // 最終更新日を更新する際の日付の設定
  80. - (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
  81. return [NSDate date];
  82. }
  83. @end
EGOSampleViewController.m

【4】サンプルプロジェクトの実行

 では、実行してみましょう。

 最初は上記のようなリストが表示されます。リストを下に引っ張るとアニメーションと同時に更新処理が走ります。

更新処理実行中

 サンプルプログラムでは更新処理を契機にリスト内のデータを変更しています。以下のようにデータが更新されれば成功です。

更新処理後

 「EGOTableViewPullRefresh」は更新と同時にテーブル内に対してデータを変更できる特性を持ったライブラリなので、今回のサンプルのようなリスト変更以外にも多様な使い方が考えられます。どのように使うかは実装するアプリ次第になるかと思います。ぜひ、いろいろと試してみてください。

Copyright © ITmedia, Inc. All Rights Reserved.

スポンサーからのお知らせPR

Smart & Social 鬮ォ�ェ陋滂ソス�ス�コ闕オ譁溷クキ�ケ譎「�ス�ウ驛「�ァ�ス�ュ驛「譎「�ス�ウ驛「�ァ�ス�ー

髫エ蟷「�ス�ャ髫エ魃会スス�・髫エ蟶キ�」�ッ闖ォ�」

注目のテーマ

4AI by @IT - AIを作り、動かし、守り、生かす
Microsoft & Windows最前線2025
AI for エンジニアリング
ローコード/ノーコード セントラル by @IT - ITエンジニアがビジネスの中心で活躍する組織へ
Cloud Native Central by @IT - スケーラブルな能力を組織に
システム開発ノウハウ 【発注ナビ】PR
あなたにおすすめの記事PR

RSSについて

アイティメディアIDについて

メールマガジン登録

@ITのメールマガジンは、 もちろん、すべて無料です。ぜひメールマガジンをご購読ください。