引き続きWindows Bridge for iOSネタです。
Windows Bridge for iOSは、Objective-Cで書かれたiOSアプリをWindowsアプリに変換するソリューションです。詳しくはこちらの記事を御覧ください。
さて今回は、UITableViewを扱えるのかを試してみたいと思います。
iOSアプリといえばUITableViewで、ほとんどのiOSアプリはUITableViewを使っているのではないでしょうか。
それくらいアプリ開発の大部分を占めるUITableViewを使っているアプリをWindows Bridge for iOSで変換してみたらどうなるのか試してみたいと思います。
Master-Detail Applicationで作成したプロジェクトを変換する
「Master-Detail Application」のウィザードを使用して作成したアプリをWindows Bridge for iOSで変換するとどうなるのか試してみました。
ナビゲーションバーに表示された[+]ボタンをタップするとアプリに現在時刻が追加されていくというサンプルアプリです。
残念ながらナビゲージョンバーまでは再現されておらず[+]ボタンそのものが表示されていませんでした。
ナビゲーションバーが表示されないので[+]ボタンをテーブル内部に表示させてみる
ナビゲーションバーが表示されていないのは仕方がないので、
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) { return 1; } else { return self.objects.count; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"button_cell" forIndexPath:indexPath]; return cell; } else { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; NSDate *object = self.objects[indexPath.row]; cell.textLabel.text = [object description]; return cell; } }
iPhoneシミュレータで起動してみました。思ったように表示されていることを確認します。
Windows Bridge for iOSを使って変換してWindowsで実行してみました。画面は白いままです。
Windows Bridge for iOS SDKではUITableView自体はサポートされているので、何が問題でセルが表示されていないのかわかりません。
セルの生成にstoryboardを使うのをやめる
storyboardを使っているのが悪いのであって、コードでUITableViewCellを生成する場合にはどうなるんだろうか?
具体的に書くと、[tableView dequeueReusableCellWithIdentifier:@"button_cell" forIndexPath:indexPath];
でUITableViewCellを生成しているのが問題なのではないか?と考えて、セルをコードで生成するように書いた。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"button_cell"]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"button_cell"]; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,100,40)]; [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [button setTitle:@"追加する" forState:UIControlStateNormal]; [button addTarget:self action:@selector(insertNewObject:) forControlEvents:UIControlEventTouchUpInside]; [cell.contentView addSubview:button]; } return cell; } else { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"]; } NSDate *object = self.objects[indexPath.row]; cell.textLabel.text = [object description]; return cell; } }
結果はNG。かなり厳しい。
現時点で最新の0.1 Preview (July 15, 2016)で初めてstoryboardがサポートされたようなので、storyboardで画面遷移及びUIパーツの配置をおこなうこと自体がダメなのかもしれませんね。
関連記事
このエントリでは、Windows Bridge for iOS (旧名: WinObjC)についてを紹介させていただきました。Windows Bridge for iOSは、Windows 10で使用できるようにブリッジさせて、Objective-CのコードやiOS APIを使ったコードを再利用するためのプロジェクトです。
この他にもWindows Bridge for iOSのネタを紹介しております。Tipsをまとめておりますのでこちらのページをご参照ください。