うーん、どうでしょう。よくわからない。とりあえず書いてみる。
UITableViewControllerってUINavigationControllerとセットで使わないとdelegateやdatasourceが渡されないようだ。週末ずっとこれで苦労していた。下記のようにinitRotViewControllerにUITableViewControllerのオブジェクトを渡さないと、タップされてもdidselectRowatIndexPathがコールバックされない。
以下、コールバックされるソース。
TableAppDelegate.h
//TableAppDelegate.h @interface TableAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; UIViewController *root; MyTableViewController *mytable; } @property (nonatomic, retain) IBOutlet UIWindow *window;
TableAppDelegate.m
//TableAppDelegate.m #import "MyTableViewController.h" @implementation TableAppDelegate @synthesize window,gridView; #pragma mark - #pragma mark Application lifecycle - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { CGRect bounds = [[UIScreen mainScreen]bounds]; window = [[UIWindow alloc]initWithFrame:bounds]; //UINavigationControllerに追加する mytable = [[[MyTableViewController alloc]init]autorelease]; root = [[UINavigationController alloc] initWithRootViewController:mytable]; [window addSubview:mytable.view]; [window makeKeyAndVisible]; return YES; }
MytableViewController
@interface MyTableViewController : UITableViewController { NSArray *cells; } @end
MyTableViewController.m
//セルの初期化処理は省略 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"called"); }
これはOK。
が、これがUIViewContorllerやUIWindowの下にaddSubviewすると、セルの初期化は問題ないのだが、コールバックが全く呼ばれない。どうやら、initRootViewControllerの中でよしなにやってくれているようだ。
で、実際の所、MyTableViewController.hをこのように変更したら解決した。
MytableViewController
@interface MyTableViewController : UITableViewController<UITableViewDelegate, UITableViewDataSource> { NSArray *cells; } @end
こうやってdelegateを渡したら、コールバックされた。
まだよくわかっていないのだけど、delegateってのはJavaでいうinterfaceで宣言されているメソッドで、それをimplしないとUITableViewのイベントが伝搬されない、という理解でいいのだろうか。