LoginSignup
10
10

More than 5 years have passed since last update.

GUIでTableViewCellの画面配置を作る(Dynamic prototypes)

Posted at

以前の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;
}
10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10