Storyboard + Auto Layout + Custom TableView Cell (xib)
Auto Layout + xibのCustom TableView Cellを使う方法
UITableViewControllerは使わずに、UIViewControllerにUITableViewを配置して、作りました。 完成品はこちら
- カスタムセルのxibは以下のような感じです。制約は縦横中央寄せで、幅は Superview.Leading==image.Leading, Superview.trailing == image.Trailing で決めて、高さは適当に image.Width : image.Height == 16 : 15 に設定しておきます。
- viewDidLoad内でカスタムテーブルヴューを登録して、テーブルヴューの行の高さを下のようにすると エラーがでて、崩れます。
UINib *cellNib = [UINib nibWithNibName:@"CustomCell" bundle:nil]; [_tableView registerNib:cellNib forCellReuseIdentifier:kReuseIdentifier]; _tableView.rowHeight = UITableViewAutomaticDimension; _tableView.estimatedRowHeight = 300; // 適当な値(300とれば全体が表示されるので)
エラーの内容は高さの制約が曖昧だと言っているようです。
Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.
カスタムセルの高さを指定してやると、エラーもなくなり、うまく表示されました。
UINib *cellNib = [UINib nibWithNibName:@"CustomCell" bundle:nil]; [_tableView registerNib:cellNib forCellReuseIdentifier:kReuseIdentifier]; _tableView.rowHeight = 300;
ソースをGitHubにアップしました。