【问题标题】:TableView Cell Row height not setting dynamicallyTableView 单元格行高未动态设置
【发布时间】:2015-08-01 11:32:23
【问题描述】:

我有一个带有 XIB 的自定义单元格类,并且我正在将单元格的自定义类的实例用于表格视图。

我有两种尺寸的单元格,一种是 220,另一种是 360。

我在运行时使用 heightForRowAtIndex 方法设置单元格的高度,

但它不适合我。

当我向下滚动时,高度未设置,并显示为

当我向上滚动时,向下的单元格显示正确的高度

像这样:

这是我的 tableview 单元格类 XIB:

,我在使用 tableviews 时遇到了非常奇怪的问题。我尝试了不同的解决方案,但没有奏效。 这是我的代码:

//code in celllForRow



        UIColor * color=[UIColor colorWithRed:102.0/255.0 green:110.0/255.0 blue:124.0/255.0 alpha:1.0];
        static NSString *CellIdentifier = @"cell";
        FBTransportersEvaluationsCell *cell = (FBTransportersEvaluationsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [FBTransportersEvaluationsCell getInstance];

        }


        if ([[[[evaluationsArray valueForKey:@"evaluationAnswer"]objectAtIndex:indexPath.row]valueForKey:@"message"] isEqual:@""] || [[[evaluationsArray valueForKey:@"evaluationAnswer"]objectAtIndex:0]valueForKey:@"message"]==(id)[NSNull null]) {

            cell.evaluationAnserSentTimeLabel.hidden=YES;
            cell.evaluationAnserSentDateLabel.hidden=YES;
            cell.evaluationAnswerSentByLabel.hidden=YES;
            cell.evaluationAnswerTextView.hidden=YES;
            cell.evaluationAnswerSingleLineView.hidden=YES;
            cell.evaluationResponceBigImg.hidden=YES;
            cell.evaluationAnswerSentDateImg.hidden=YES;
             cell.evaluationAnswerSentByTag_.hidden=YES;
            self.sendEvaluationResponceButton.hidden=NO;
            //
        }
        else
        {
            cell.evaluationAnserSentTimeLabel.hidden=NO;
            cell.evaluationAnserSentDateLabel.hidden=NO;
            cell.evaluationAnswerSingleLineView.hidden=NO;
            cell.evaluationResponceBigImg.hidden=NO;
            cell.evaluationAnswerSentByLabel.hidden=NO;
            cell.evaluationAnswerTextView.hidden=NO;
            self.sendEvaluationResponceButton.hidden=YES;
            cell.evaluationContentBigImage.image=[UIImage imageNamed:@"responce-img.png"];
            cell.evaluationAnswerTextView.text=[NSString stringWithFormat:@"\"%@ \" ",[[[evaluationsArray valueForKey:@"evaluationAnswer"]objectAtIndex:indexPath.row]valueForKey:@"message"]];
             cell.evaluationAnswerSentByLabel.text=[NSString stringWithFormat:@"%@",[[FBUserManager sharedUserManager]userName]];

            NSString * str = [NSString stringWithFormat:@"%@",[[[evaluationsArray valueForKey:@"evaluationAnswer"]objectAtIndex:indexPath.row]valueForKey:@"date_evaluation_answer"]];
            NSArray * arr = [str componentsSeparatedByString:@" "];
            cell.evaluationAnserSentDateLabel.text=[arr objectAtIndex:0];
            cell.evaluationAnserSentTimeLabel.text=[arr objectAtIndex:1];

        }


/// code in heightForRow


  if ([[[[evaluationsArray valueForKey:@"evaluationAnswer"]objectAtIndex:indexPath.row]valueForKey:@"message"] isEqual:@""] || [[[evaluationsArray valueForKey:@"evaluationAnswer"]objectAtIndex:0]valueForKey:@"message"]==NULL) {

              return 220;
        }
        else
        {
           return  360;



        }

最后是我的问题的 15 秒视频链接:Video link

【问题讨论】:

  • 从视频中,您的所有单元格似乎都是 360 度高度。看起来有些白色背景的视图挡住了单元格的顶部。你可以尝试设置cell.contentView.backgroundColor,甚至tableview的backgroundColor,看看是否真的有这样的阻塞视图。

标签: xcode uitableview ios8 autolayout uicollectionviewcell


【解决方案1】:

请按照您的要求设置行高,如下所示,

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
      if(condition 1)
      {
        return height1;
      }
      else
     {
        return height2;
     }

}

并检查您的情况。你也可以参考http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift这个。

【讨论】:

  • 你没有正确阅读问题@jashu,我已经按照你的要求做了这个
  • 您必须根据您的内容计算单元格高度。通过使用 - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode。之后,您可以获得内容大小。然后您必须在上述条件下使用该尺寸。
猜你喜欢
  • 2016-10-19
  • 2017-11-14
  • 2023-03-23
  • 2021-12-23
  • 1970-01-01
  • 2019-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多