UITableView | にゃろいち。

にゃろいち。

2019年9月生まれの娘の父親目線での育児記録、幼児教育記録などが中心です。世の中で見かけるにゃろいちはほぼ私です。

いまUITableViewをさわり中。

他の部品と同じようにStoryboardのViewの中にTable Viewを置いて、Table View Cellとかいうのを中に入れて、それで表示されると思ったら、なぜか出ない。


どうやらUITableViewはいくつか設定をしないと表示することすらできないっぽいです。
しかも苦手なデリゲート的なやつ。
UITableViewControllerってのを使うと、そういうのが最初から実装されてるとあったけど、Storyboardに入れたら画面全体のTableしか出てこなくて先に進まず断念。

結局EquipmentViewController.m (Table Viewを作りたいViewが乗ってるViewController) の中でStoryboard使わずに作ることに。

必要な設定っていうのが、UITableViewが持ってるデリゲートメソッドのうち2つは実装必須なのと、DataSourceとかいうのも実装?しないといけないようです。
とりあえずEquipmentViewControllerで実装を請け負えばいいみたいで、.hの
@interface EquipViewController : UIViewController
のうしろにを足しました。これが実装してますよっていう印になるらしい。

あと、デリゲート先がEquipmentViewControllerだよって知らせるために、
equipTable.delegate = self;
equipTable.dataSource = self;
を追加。equipTableは作ったUITableViewの名前。この時点ではdataSourceのほうは何やってるのかよくわからないまま。

で、.mのほうで、実装必須らしい下の2つを作りました。
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
1個目はTableの要素数を決めるやつで、2個目はセルの中身を決めるやつらしい。
いろいろ他のサイトを見つつこの2つを実装。

どうやらEquipmentViewが表示された時点(equipTableが表示される時点)でこいつらが呼ばれるようです。1画面に2個以上Tableがある場合に同じ内容になってしまったので、equipTable.tag = 1 とかやって番号制にして、ifとかswitchとかで書き分けたらうまく行った。
つまり1個目はTableの数だけ、2個目は要素の数だけ何回も呼ばれるっぽいです。

今回はスクロールしない、要素が最初から全部見えてるやつも作りたかったのに、作ったやつがスクロールする。。ちょっと困ったけど、equipTable.ScrollEnabled = NO; にすればいいことに気づいた。
ヘッダはなぜか一緒にスクロールするから、UILabelを置きました。ヘッダが動かないようにする方法もあるらしいけど、面倒くさくなっちゃいましたw








ちなみに試行錯誤の上できた2つのメソッドと、viewDIdLoad
ちゃんと書けてる保証はないけど、とりあえずシミュレータ上には見えるようにはなりました
(equipTableとitemTableっていう2つのTableViewがある)

- (void)viewDidLoad
{
[super viewDidLoad];

UITableView *equipTable = [[UITableView alloc] initWithFrame:CGRectMake(40, 100, 240, 100) style:UITableViewStyleGrouped];
equipTable.delegate = self;
equipTable.dataSource = self;
equipTable.backgroundColor = self.view.backgroundColor;
dataSourceOfEquipmentTable = [[NSArray alloc] initWithObjects:@"装備1",@"装備2",nil];
equipTable.ScrollEnabled = NO;
[self.view addSubview:equipTable];

UITableView *itemTable = [[UITableView alloc] initWithFrame:CGRectMake(40, 240, 240, 160) style:UITableViewStyleGrouped];
itemTable.delegate = self;
itemTable.dataSource = self;
itemTable.backgroundColor = self.view.backgroundColor;
dataSourceOfItemTable = [[NSArray alloc] initWithObjects:@"アイテム1",@"アイテム2",@"アイテム3",nil];
[self.view addSubview:itemTable];

equipTable.tag = 1;
itemTable.tag = 2;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch(tableView.tag){
case 1:
return dataSourceOfEquipmentTable.count;
case 2:
return dataSourceOfItemTable.count;
default:
return 0;
}
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}

NSString *name;
switch(tableView.tag){
case 1:
name = [dataSourceOfEquipmentTable objectAtIndex:indexPath.row];
break;
case 2:
name = [dataSourceOfItemTable objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
default:
return nil;
}
cell.textLabel.text = name;
return cell;
}


2015/12 追記
練習で作っていたアプリを遊べるように改良してリリースしてみました!よかったら遊んでみてください。
{EBAFE2B1-E4BF-408C-9FFB-E17A16CD1967:01}





Xcode 4 完全攻略
Xcode 4 完全攻略
posted with amazlet at 12.09.27
STUDIO SHIN
ソフトバンククリエイティブ
売り上げランキング: 10379