2016-03-04 142 views
1

我想增加基于内容的tableview单元格和tableview高度。 假设tableview包含2条记录,他的第一个单元格高度是100,第二个单元格高度是25,那么tableview高度应该是100 + 25 = 125。 如何实现此功能?如何在iOS中使用动态tableview高度创建动态tableview单元格

在此先感谢。

+0

什么是你的主要目标,您要根据各小区的高度来计算表的高度,或者你只是想创建具有不同高度细胞的表? –

+0

我只是想根据标签内容和基于单元高度的tableview高度创建单元格的高度,这两者。 –

+0

使用自动布局 http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – Maverick

回答

2

你绝对可以做到这一点,

  1. 首先确保您的子视图必须设置为从上到下顺序来计算电池所需的高度细胞的约束。

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

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

  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. 呼叫,当你准备与数据源的表,并调用方法

    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]; 
    }); 
    
-3
self.tableView.frame = CGRectMake(self.tableView.origin.x, self.tableView.origin.y, self.tableView.frame.size.width, 125); 
+0

我只是解释一下我究竟想要什么,我不能设置像125一样的静态高度。 –

+1

尽管这段代码片段可以解决这个问题,[包括解释](http://meta.stackexchange.com/questions/114762/解释完全基于代码的答案)确实有助于提高帖子的质量。请记住,您将来会为读者回答问题,而这些人可能不知道您的代码建议的原因。 – SmokeDispenser

+0

Jan Greve的+1。我解释了你的问题后回答你的问题:) 所以,如果你找不到解决方案,与我分享你的联系方式 – skillerman14

1

如果您正在运行iOS 8+, 您可以使用此方法:

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

属性。

  1. 此生效,您应该没有任何高度heightForRowAtIndexpath设置
  2. 应设置单元格的限制,即,限制了目前内细胞的元素,所以设定的约束是不够的tableviewcell计算它在运行时高度
0

解决方案在迅速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)) 
     } 
} 
相关问题