【问题标题】:Multiple Prototype Cells are showing fixed height多个原型单元显示固定高度
【发布时间】:2017-11-03 19:39:02
【问题描述】:

我是 IOS 新手,我正在尝试在 UITableView 中制作多个原型单元格,但我总是得到相同大小的行,不知道为什么会这样。我已经搜索了很多!但没有线索,任何帮助对我来说都会很棒。提前致谢。This is the image for what I am doingthis 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;
     }
 }

【问题讨论】:

  • 请注意,任何instoryboard 的大小都没有任何意义:它只是您工作时间的一个指标。关于表格视图和单元格,您可能需要了解 tableView#heightForRowAt,享受!
  • @Fattie 你的意思是说我必须重写这个方法并为每一行指定高度?
  • 当然,我会指导您阅读有关表格视图的任何教程。假设您有两种“类型”的行(假设一种是图像,一种是文本消息)。所以,这是完全正常的:在调用 tableView#heightForRowAt 时,您基本上会说“如果图像,返回 190,如果消息返回 75” - 或其他任何内容
  • 为了清楚起见,在当今所有现实世界的 iOS 工程中,您都使用自动布局。但是为了让您开始学习曲线,请继续尝试 tableView#heightForRowAt - 享受
  • 等等——你在用objectiveC吗?!你真的在浪费你的时间。只需单击即可在 Swift 中开始一个新项目,并享受您的学习体验! “ray wenderlich”教程对于爱好者来说是一个不错的起点,尽情享受吧! tableView#heightForRowAt

标签: ios objective-c uitableview storyboard row-height


【解决方案1】:

你没有正确设置你的 layoutconstraint。

怎么做?

请记住至少在您的单元格中的一个出口中添加顶部和底部约束。就像这样。

并且 在你的视图控制器的 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

【讨论】:

  • 我已经添加了这些代码行,但它会导致滚动问题,我的意思是这些代码行会进行一些昂贵的调用,从而导致性能问题。
  • 没有昂贵的电话,也没有副作用。请交叉检查你的代码。
【解决方案2】:

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

-(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

【讨论】:

  • 你能添加一些关于给定代码的解释以便更好地理解吗?
  • @user2595453 你能告诉我如何计算所需的高度吗???因为将来我可能想添加更多标签和按钮,标签可能是动态高度???
  • @Najam 实现委托方法 - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0) { return 140.0;将 tableview.rowHeight = UITableViewAutomaticDimension 放入 viewdidload 中,对单元格中的所有子视图应用适当的约束,以便在内容增加时单元格可以增长。
  • 我附上了它对我有用的示例的屏幕截图以获取动态内容,请参阅屏幕截图。
猜你喜欢
  • 2019-10-12
  • 1970-01-01
  • 1970-01-01
  • 2014-11-11
  • 1970-01-01
  • 2018-06-12
  • 1970-01-01
  • 2010-12-16
  • 1970-01-01
相关资源
最近更新 更多