TableView单元格行高度未动态设置

时间:2015-05-20 09:40:33

标签: xcode uitableview ios8 autolayout uicollectionviewcell

我有一个带有XIB的自定义单元类,我正在使用单元格自定义类的实例进行查看。

我有两种尺寸的细胞,一种是220,另一种是360.

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

但它不适合我。

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

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

像这样:enter image description here

这是我的tableview单元格类XIB:

enter image description here

,我面对使用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

1 个答案:

答案 0 :(得分:0)

请根据您的要求设置行高,如下所示

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