2010/03/25

LeopardにRailsの環境を作る

railsの環境設定 備忘録
MacOSX(10.5) LeopardでRailsの環境を整える。
Leopardには、最初からRailsが入っていますが、アップデートしないと不具合がでることが多い。
HFRailのscaffoldでつまずいたのもここ。
scaffold を実行して、
wrong constant name Name:stringController
とかが出たらアップデートすること。作ったプロジェクトもいったん破棄する。

以下を順に実行。
$ sudo gem update --system
$ sudo gem install rails
$ sudo gem update rake
$ sudo gem update sqlite3-ruby

2010/03/07

Dropboxを使って、複数のPCでのGoogle App Engineのアプリ開発の同期を取る

Google App Engineの開発ディレクトリをDropbox上に置くことで複数のPCで開発が行えるようになる。

例えばSomeAppというアプリを開発しているとして、

user/Dropbox/SomeApp

というようにDropbox上に開発ディレクトリを作ってここで開発を行う。
Dropboxの上にあるので編集後に勝手に同期される。

ただしこのディレクトリを直接App Engine Lancherで指定してもダメで、ここから別のローカルディレクトリにリンクを張って、そこをApp Engine Lancherに指定させる。

以下のディレクトリにリンクをはってApp Engine Lancherに指定させるとちゃんと起動する。
user/develop/gae/SomeApp

$cd develop/gae/
$ln -s ~/Dropbox/SomeApp SomeApp

2010/02/01

iPhoneSDK 実行時に急に落ちる症状

アプリがいきなり落ちるパターンのバグは、メモリリーク系。retainカウントの間違いや、releaseの重複があやしい

2010/01/14

iPhone : CoreDataレシピ カスタムのプロトコルを作成する。

レシピのAddをpresentModalで行うときに、カスタムのプロトコルを作成して呼び出し元をそれに準拠させておく。

呼出し元クラス(カスタムのプロトコルに準拠)
やること:
1.プロトコルの設定
2.プロトコルメソッドの実装

呼出しクラス(カスタムのプロトコルを定義)
やること:
1.プロトコルの宣言
2.delegateの宣言
3.プロトコルメソッドの宣言



カスタムのプロトコルの使い方
呼出しクラス

やること:
1.プロトコルの宣言
2.delegateの宣言
3.プロトコルメソッドの宣言

  1. //プロトコルの宣言  
  2. @protocol RecipeAddDelegate;  
  3. @class Recipe;  
  4.   
  5. @interface RecipeAddViewController : UIViewController <uitextfielddelegate> {  
  6.     @private  
  7.         Recipe *recipe;  
  8.         UITextField *nameTextField;  
  9. //delegateを宣言  
  10.         id <recipeadddelegate> delegate;  
  11. }  
  12.   
  13. @property(nonatomic, retain) Recipe *recipe;  
  14. @property(nonatomic, retain) IBOutlet UITextField *nameTextField;  
  15. @property(nonatomic, assign) id <recipeadddelegate> delegate;  
  16.   
  17. - (void)save;  
  18. - (void)cancel;  
  19.   
  20. @end  
  21.   
  22.   
  23. //プロトコルのメソッドを定義  
  24. @protocol RecipeAddDelegate <nsobject>  
  25. // recipe == nil on cancel  
  26. - (void)recipeAddViewController:(RecipeAddViewController *)recipeAddViewController didAddRecipe:(Recipe *)recipe;  
  27.   
  28. @end  
  29.   
  30. </nsobject></recipeadddelegate></recipeadddelegate></uitextfielddelegate>  

実装部は長いのでいるとこだけ。
プロトコルメソッドを呼び出すだけ、実装はしなくてよい。実装はプロトコルに準拠した呼出しもとクラスで行う。
これでプロトコルを経由して呼出し元のメソッドを呼び出す。

  1. - (void)save {  
  2.       
  3.     recipe.name = nameTextField.text;  
  4.   
  5.  NSError *error = nil;  
  6.  if (![recipe.managedObjectContext save:&error]) {  
  7.   NSLog(@"Unresolved error %@, %@", error, [error userInfo]);  
  8.   abort();  
  9.  }    
  10.     //プロトコルを経由してメソッドを呼び出す。メソッドの実装は呼出し元で  
  11.  [self.delegate recipeAddViewController:self didAddRecipe:recipe];  
  12. }  




呼出し元

やること:
1.プロトコルの設定
2.プロトコルメソッドの実装

インターフェース
  1. #import "RecipeAddViewController.h"  
  2.   
  3. @class Recipe;  
  4. @class RecipeTableViewCell;  
  5.   
  6. @interface RecipeListTableViewController : UITableViewController <recipeadddelegate, nsfetchedresultscontrollerdelegate=""> {  
  7.     @private  
  8.         NSFetchedResultsController *fetchedResultsController;  
  9.         NSManagedObjectContext *managedObjectContext;  
  10. }  
  11.   
  12. @property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;  
  13. @property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;  
  14.   
  15. - (void)showRecipe:(Recipe *)recipe animated:(BOOL)animated;  
  16. - (void)configureCell:(RecipeTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;  
  17.   
  18. @end  
  19. </recipeadddelegate,>  

実装部
プロトコルのメソッドを実装する。

  1. #pragma mark -  
  2. #pragma mark Recipe support  
  3.   
  4. - (void)add:(id)sender {  
  5. //ここは呼出しメソッド。ここから呼出しクラスを起動してdelegateする。  
  6.     RecipeAddViewController *addController = [[RecipeAddViewController alloc] initWithNibName:@"RecipeAddView" bundle:nil];  
  7.     addController.delegate = self;  
  8.    
  9.  Recipe *newRecipe = [NSEntityDescription insertNewObjectForEntityForName:@"Recipe" inManagedObjectContext:self.managedObjectContext];  
  10.  addController.recipe = newRecipe;  
  11.   
  12.     UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addController];  
  13.     [self presentModalViewController:navigationController animated:YES];  
  14.       
  15.     [navigationController release];  
  16.     [addController release];  
  17. }  
  18.   
  19. //プロトコル実装部 dismissModalViewControllerでモーダルを除去する。  
  20. - (void)recipeAddViewController:(RecipeAddViewController *)recipeAddViewController didAddRecipe:(Recipe *)recipe {  
  21.     if (recipe) {          
  22.         // Show the recipe in a new view controller  
  23.         [self showRecipe:recipe animated:NO];  
  24.     }  
  25.       
  26.     // Dismiss the modal add recipe view controller  
  27.     [self dismissModalViewControllerAnimated:YES];  
  28. }  
  29.   
  30.   
  31. - (void)showRecipe:(Recipe *)recipe animated:(BOOL)animated {  
  32.     // Create a detail view controller, set the recipe, then push it.  
  33.     RecipeDetailViewController *detailViewController = [[RecipeDetailViewController alloc] initWithStyle:UITableViewStyleGrouped];  
  34.     detailViewController.recipe = recipe;  
  35.       
  36.     [self.navigationController pushViewController:detailViewController animated:animated];  
  37.     [detailViewController release];  
  38. }  

iPhone : 編集可能なテーブルの作成

CoreData Resipeサンプルより。
iPhone : CoreData レシピサンプルを探る

編集可能なテーブルを作るには、
編集可能なテーブルビューセルを作成して使用する。
実装はほとんどなくて、Interface Bulderメインで作成する。セルの操作のためのIBOutletプロパティを設定するだけ。

IB側はこんな感じ



labelとtextfieldになるセルの部分だけを作成する。

手順
1:クラスの作成


新規ファイル>NSObject>種類はUITableViewCellのサブクラス
ViewControllerクラスで作っちゃだめです。
2:xibの作成


新規ファイル>View XIB

3:xibを編集
デフォルトのViewを削除しTableViewCellに変更。

TableViewCellのクラスをEditingTableViewクラスに変更
TableViewCellを配置して、labelとtextFieldを追加する。


TableViewCellのアウトレットにlabelとtextFieldを接続。


File's OwnerのNew Referencing OutletにtextFieldのdelegateを接続する。

後はテーブルビューコントローラーを作って、File's Ownerのクラスに設定する。
さらにFile's Owner経由でテーブルコントローラーのプロパティにEditingTableViewCellを接続する。
テーブルビューコントローラークラス内にXIBを直接読み込んで使用する。
バンドルからXIBを読み込んで、EditiongTableViewCellクラスのViewへ接続する。

編集可能セルとテーブルビューは1セットということになる。複数箇所で使う場合は複数セット用意することになりそう。


EditingTableViewCellクラス
EditingTableViewCellクラスの中身はこんだけ
  1. //インターフェース部  
  2. @interface EditingTableViewCell : UITableViewCell {  
  3.  UILabel *label;  
  4.  UITextField *textField;  
  5. }  
  6.   
  7. @property (nonatomic, retain) IBOutlet UILabel *label;  
  8. @property (nonatomic, retain) IBOutlet UITextField *textField;  
  9.   
  10. @end  
  11.   
  12. //実装部  
  13. #import "EditingTableViewCell.h"  
  14.   
  15. @implementation EditingTableViewCell  
  16.   
  17. @synthesize label, textField;  
  18.   
  19. - (void)dealloc {  
  20.  [label release];  
  21.  [textField release];  
  22.  [super dealloc];  
  23. }  
  24.   
  25. @end  

これを使いたいテーブルビューコントローラーのtableView:cellForRowAtIndexPathメソッドで設定してあげればよい。

Table View Controllerクラス
  1. //インターフェース側  
  2. #import <uikit uikit.h="">  
  3.   
  4. @class EditingTableViewCell;  
  5.   
  6. @interface AnkiEditTableViewController : UITableViewController {  
  7.  @private  
  8.   //なぜプライベートで宣言するのか?  
  9.   EditingTableViewCell *editingTableViewCell;  
  10. }  
  11. @property (nonatomic, assign) IBOutlet EditingTableViewCell *editingTableViewCell;  
  12.   
  13. @end  
  14.   
  15. //実装部  
  16. //全体は省略して必要なところだけ  
  17. //エディットに入れたいので、セレクトさせないようにする。  
  18. - (void)viewDidLoad {  
  19.     [super viewDidLoad];  
  20.  self.tableView.allowsSelection = NO;  
  21.  self.tableView.allowsSelectionDuringEditing = NO;  
  22. }  
  23.   
  24. // セルに設定する  
  25. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  26.  static NSString *cellIdentifire = @"EDITOR_TABLEVIEWCELL";  
  27.  EditingTableViewCell *cell = (EditingTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifire];  
  28.   if (cell == nil) {  
  29.    //バンドルからXIBの読み込み  
  30.    [[NSBundle mainBundle] loadNibNamed:@"EditingTableViewCell" owner:self options:nil];  
  31.    //IB上で接続されている。  
  32.    cell = editingTableViewCell;  
  33.    self.editingTableViewCell = nil;  
  34.   }  
  35.    
  36.     return cell;  
  37. }  
  38.   
  39. </uikit>  
EditingTableViewCellはIBOutletとインターフェースを毎回変更すれば、ほとんどそのままで使い回せる。
単純に編集可能なテーブルを生成するだけなら、毎回変更しなくてもこのままでもいい。ビューコントローラークラスの指定を変更するだけでよい。
これくらい公式で用意しておいてくれると助かるのに。

iPhone : CoreData レシピサンプルを探る


iPhone Dev Center CoreRecipes Sample


以外といろいろな物に使えそうなサンプル。
テーブルレイアウトとCoreDataの基本やTipsがいっぱい詰まっている印象です。
CoreData周りの曖昧に理解していた部分をすっきりさせるのに最適です。

・NSManagedObjectの取り扱い方
NSManagedObjectクラスのサブクラスのResipeクラスを作って使う。

・テーブルのセルの拡張方法
TableViewCellをカスタムで拡張する。

・編集可能なテーブルを作る
編集するセル用にEditingTableViewCellクラスを作って使う。

2010/01/05

編集時のアクセサリ設定。

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  2.       
  3.  static NSString *SetCellIdentifier = @"SetCellIdentifier";  
  4.    
  5.  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SetCellIdentifier];  
  6.    
  7.  if (cell  == nil) {  
  8.   cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SetCellIdentifier];  
  9. //ここでアクセサリと編集時のアクセサリを設定する。  
  10.   cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;  
  11.   cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;  
  12.  }  
  13.    
  14.  cell.textLabel.text =[[setListArray objectAtIndex:indexPath.row] valueForKey: @"set_title"];  
  15.  return cell;  
  16. }