如何在iOS中创建具有动态tableview高度的动态tableview单元格

时间:2016-03-04 03:59:40

标签: ios objective-c iphone uitableview

我想根据内容增加tableview单元格和tableview高度。 假设tableview包含2条记录,他的第1个单元格高度为100,第2个单元格高度为25,则tableview高度应为100 + 25 = 125。 如何实现这一功能?

提前致谢。

4 个答案:

答案 0 :(得分:2)

你绝对可以这样做,

  1. 首先确保您的单元格子视图的约束必须设置为从上到下,以便计算单元格所需的高度。

  2. 确保您的委托设置如下

    -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
          return 44;
    } 
    
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return UITableViewAutomaticDimension;
    }
    
  3. 设置tableView的高度约束并使该约束出口。

  4. 将以下方法添加到您想要动态调整tableView大小的类中。

    - (void)adjustHeightOfTableview
    {
    
        CGFloat height = self.tableView.contentSize.height;
       //CGFloat maxHeight = self.tableView.superview.frame.size.height - self.tableView.frame.origin.y;
    
       /* 
         Here you have to take care of two things, if there is only    tableView on the screen then you have to see is your tableView going below screen using maxHeight and your screen height,
         Or you can add your tableView inside scrollView so that your tableView can increase its height as much it requires based on the number of cell (with different height based on content) it has to display.
      */
    
      // now set the height constraint accordingly
      self.constraintHeightTableView.constant = height;
    
     //If you want to increase tableView height with animation you can do that as below.
    
     [UIView animateWithDuration:0.5 animations:^{
           [self.view layoutIfNeeded];
     }];
    }
    
  5. 准备好表格的dataSource后调用此方法,并将方法调用为

    dispatch_async(dispatch_get_main_queue(), ^{
    
        [self.tableView reloadData];
    
        //In my case i had to call this method after some delay, because (i think) it will allow tableView to reload completely and then calculate the height required for itself. (This might be a workaround, but it worked for me)
        [self performSelector:@selector(adjustHeightOfTableview) withObject:nil afterDelay:0.3];
    });
    

答案 1 :(得分:1)

如果您运行的是iOS 8+, 您可以使用:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 80 // your desired or expected height

性质。

  1. 要使此生效,您不应在heightForRowAtIndexpath
  2. 中设置任何高度
  3. 您应该设置单元格约束,即单元格内部元素的约束,因此设置约束足以让tableviewcell在运行时计算它的高度

答案 2 :(得分:0)

swift 3中的解决方案

第1步。 设置顶部,底部,前导和尾随约束(不要使其恒定高度,但是现在设置一个恒定的高度约束)。 现在我们将根据细胞数量及其高度动态改变这个高度。

第2步。 将该高度约束的出口拖到该类,并将该函数复制到该类中的任何位置。

    func adjustHeightOfTableview() {
    let height: CGFloat = tableview.contentSize.height
    self.outLetOfTheHeightConstraint.constant = height

}

第3步。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    DispatchQueue.main.async {
            self.tableview.reloadData()
            self.perform(#selector(self.adjustHeightOfTableview))
        }
}

答案 3 :(得分:-3)

self.tableView.frame = CGRectMake(self.tableView.origin.x, self.tableView.origin.y, self.tableView.frame.size.width, 125);
相关问题