以前のXCode(InterfaceBuilder)では、UITableViewCellのViewを作る事は出来なかった(はず、記憶違いかも)のですが、最近のXCodeのStoryBoardでは"Dynamic Prototypes"という機能で、GUIからTableCellViewを作る事が出来るようになっていました。
これが使えれば、GUIでTableViewCellの配置等も調整出来て便利そうです。
- StoryBoard上でUITableViewControllerの"Attribute inspector"->"Table view"の中に、"Content: Dynamic Prototypes"という指定がある。
- その下の"Prototype cells"は表示する個数
- このTableViewの中のTableViewCellに"identity"を設定しておけば、Objective-Cからアクセスが出来る
- Obj-Cからは、以下のコードで取得出来る。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell;
}
- StoryBoard上で、セルに対応する自作クラスも設定できるので、そこで自作クラスのIBOutletに項目を関連付けしておく。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell1";
PATableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.lavel.text=[NSString stringWithFormat:@"%d",indexPath.row];
return cell;
}