多个原型单元显示固定高度

Multiple Prototype Cells are showing fixed height

我是 IOS 的新手,我试图在 UITableView 中制作多个原型单元格,但我总是得到相同大小的行,不知道为什么会这样。我找了很多!但没有线索,任何帮助对我来说都是很好的。提前致谢。This is the image for what I am doing and this is what I am getting as output。

这是我的 DataSource 方法代码....

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
   (NSInteger)section
 {
    return 12;
 }

- (UITableViewCell *)tableView:(UITableView *)tableView 
   cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
     NSInteger count = indexPath.row;
     if(count == 0)
     {
         DetailPostCell *Cell1 = (DetailPostCell *)[tableView 
          dequeueReusableCellWithIdentifier:@"Cell1" 
            forIndexPath:indexPath];
       return  Cell1;
    }
    else
    {
         CommentCell *Cell2 = (CommentCell *)[tableView 
          dequeueReusableCellWithIdentifier:@"Cell2" 
            forIndexPath:indexPath];
         return Cell2;
     }
 }

您没有正确设置布局约束。

怎么办?

记得在你的 cell.just 中至少在 one outlet 中添加 top 和 bottom 约束。

还有 在您的视图控制器的 viewDidLoad.write 这两行中。

self.tableView.estimatedRowHeight = 88.0//your estimated height.don't worry it will take what your custom cell want.:)
self.tableView.rowHeight = UITableViewAutomaticDimension

实现用于设置原型单元格高度的委托方法,在这种情况下,第一个单元格需要与其他单元格不同的高度。如果您 return 每个单元格的高度正确,单元格的内容就会正确显示。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == 0) {
        return  //Desired hight of your first cell;
    }
    else {
        return  //Desired hight of your rest cells;
    }
}

result screen shot of comment mention below