Ios UITableViewCell仅在滚动上正确布局

Ios UITableViewCell仅在滚动上正确布局,ios,iphone,objective-c,uitableview,Ios,Iphone,Objective C,Uitableview,我有一个自定义UITableView单元格的UITableView。根据我从服务器收到的文本,单元格需要具有不同的高度。我将高度相应更改如下: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) return [self getHeightOfQuestion]; else i

我有一个自定义
UITableView单元格的
UITableView
。根据我从服务器收到的文本,单元格需要具有不同的高度。我将高度相应更改如下:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
        return [self getHeightOfQuestion];
    else if (indexPath.row == 1)
        return 64.0f;
    else // ****** this is the cell in question!
        return [self getHeightOfAnswerAtIndexPath:indexPath];
    return 300.0f;
}

]- (CGFloat) getHeightOfAnswerAtIndexPath:(NSIndexPath*) indexPath
{
    CGRect answerFrame = CGRectZero;
    if ([QXTUtility currentOsVersion] >= 7.0f)
    {
        answerFrame = [[[[self.answerDetails objectForKey:kAnswers] objectAtIndex:indexPath.row-2] objectForKey:kAnswerText] boundingRectWithSize:CGSizeMake(242.0f, 900.0f) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-Light" size:14.0f]} context:nil];
    }
    else
    {
        CGSize answerSize = [[[[self.answerDetails objectForKey:kAnswers] objectAtIndex:indexPath.row-2] objectForKey:kAnswerText] sizeWithFont:[UIFont fontWithName:@"Roboto-Light" size:14.0f] constrainedToSize:CGSizeMake(242.0f, 500.0f) lineBreakMode:NSLineBreakByTruncatingTail];
        answerFrame.size = answerSize;
    }
    NSLog(@"Frame %@", NSStringFromCGRect(answerFrame));

    return answerFrame.size.height + 50;
}
它在iOS6上运行良好。但在iOS7中,我得到以下输出:

但是,如果我再次滚动UITableView,单元格的布局将非常完美,如下所示:

这是我的
cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *answerCellIdentifier = @"QXTAnswerCell";

    QXTAnswerCell *answerCell = (QXTAnswerCell *)[tableView dequeueReusableCellWithIdentifier:answerCellIdentifier];
    if (answerCell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"QXTAnswerCell" owner:self options:nil];
        answerCell = [nib objectAtIndex:0];
    }
    [self setDataToAnswerCell:answerCell AtIndexPath:indexPath];
    [self layoutAnswerCell:answerCell AtIndexPath: indexPath];
    [answerCell setNeedsLayout];
    return answerCell;
}

请帮帮我。一旦答案的数据源被填充,我将调用重新加载数据。 谢谢

更新

刚刚发现,一旦从服务器接收到答案,就不会调用第一个
reloadData

// Delegate from Network Class
- (void) answersReceivedWithDetails:(id)answerDetails
{
    NSLog(@"Answer Details %@", answerDetails);
    self.answerDetails = [[NSMutableDictionary alloc] initWithDictionary:answerDetails];
    [self.tableView reloadData];
    [self.tableView setNeedsDisplay];
}
以下是单元格的约束条件:


我认为问题在于在tableView中返回正确的高度:heightForRowAtIndexPath:

它在indexPath.row=1时始终返回64.0f

static CGFloat answerCellDefaultHeight = 200.0f; //change this to the actual value of the height of the answer table cell

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return [self getHeightOfQuestion];
    } else if (indexPath.row == 1) {
        return 64.0f; 
    } else {
        CGFloat computedHeight = [self getHeightOfAnswerAtIndexPath:indexPath];
        return (computedHeight > answerCellDefaultHeight) ? computedHeight : answerCellDefaultHeight;
    }
}

以下是我为解决我的问题所做的。使用IBOutlets创建的
boundingrectizewithsize
UILabels
存在一些问题。因此,我做了上述操作,然后将其分配给使用代码创建的
UILabel
,并将其添加到单元格中。一切都很顺利

- (void) layoutAnswerCell:(QXTAnswerCell*) answerCell
              AtIndexPath:(NSIndexPath*) indexPath
{
    if ([QXTUtility currentOsVersion] >= 7.0f)
    {
        [answerCell.answer removeFromSuperview];
        NSString *answerString = <your string>
        CGRect answerFrame = [answerString boundingRectWithSize:CGSizeMake(242.0f, 900.0f) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-Light" size:14.0f]} context:nil];
        answerFrame.origin.x = 20.0f;

        answerFrame.origin.y = answerCell.name.frame.origin.y + answerCell.name.frame.size.height + 6;
        answerFrame.size.height = ceilf(answerFrame.size.height);
        answerFrame.size.height += 30.0f;
        answerCell.answer.lineBreakMode = NSLineBreakByTruncatingTail;
        answerCell.answer.frame = answerFrame;

        UILabel *newLabel = [[UILabel alloc] initWithFrame:answerFrame];
        [newLabel setFont:[UIFont fontWithName:@"Roboto-Light" size:14.0f]];
        newLabel.text = answerString;
        newLabel.numberOfLines = 0;
        [answerCell.contentView addSubview:newLabel];
        [answerCell setNeedsDisplay];
    }
}
-(无效)布局应答器:(QXTAnswerCell*)应答器单元
AtIndexPath:(nsindepath*)indepath
{
如果([QXTUtility currentOsVersion]>=7.0f)
{
[answerCell.answer removeFromSuperview];
NSString*应答字符串=
CGRect answerFrame=[answerString BoundingDirectWithSize:CGSizeMake(242.0f,900.0f)选项:NSStringDrawingUserLineFragmentOrigin属性:@{NSFontAttributeName:[UIFontWithName:@“Roboto Light”大小:14.0f]}上下文:nil];
answerFrame.origin.x=20.0f;
answerFrame.origin.y=answerCell.name.frame.origin.y+answerCell.name.frame.size.height+6;
answerFrame.size.height=ceilf(answerFrame.size.height);
应答器框架尺寸高度+=30.0f;
answerCell.answer.lineBreakMode=nslinebreakbytruncingtail;
answerCell.answer.frame=answerFrame;
UILabel*newLabel=[[UILabel alloc]initWithFrame:answerFrame];
[newLabel setFont:[UIFont fontWithName:@“Roboto Light”大小:14.0f];
newLabel.text=应答字符串;
newLabel.numberOfLines=0;
[answerCell.contentView addSubview:newLabel];
[应答单元设置需要显示];
}
}

看看这是否适用于其他人。如果是这样的话,我会接受这个答案。但是它确实为我完成了工作。

你在哪里重新加载tableView?在问题中更新。我在那里碰到了一个断点。但不调用CellForRowatineXpath。也尝试在主线程中调用它。仍然没有luckDo您有ViewController或TableViewControllerTableViewController,但单元格来自NIB。我认为您应该在QXTAnswerCell的
-(void)layoutSubviews
方法中布局单元格的子视图,而不是在视图控制器中。indexPath.row=1的高度恒定为64.0f。它下面的其余单元格具有动态高度。高度恢复正常。但只有一次,我将表格滚动了几次。其他情况下,框架未扩展到所需的大小。包含文本“Mwonnee Van!”的单元格是indexPath.row=1,对吗?如果是这样,那么它总是返回64.0f的高度。实际上,这是第三个单元格(indexPath.row==2)。上面还有两个单元格。但是那些包含图片什么的。好吧,截图是模糊的。但是您可以考虑查看方法“LayOutAsSeriCy:AtdixPATH:”。顺便说一句,我已经更新了我的答案。如果您可以提供“QXTAnswerCell”的详细信息,则问题的解决方案可能会缩小。QXTAnswerCell实际上是空的,因为与布局或任何内容无关。我已经添加了上述约束的屏幕截图。
- (void) layoutAnswerCell:(QXTAnswerCell*) answerCell
              AtIndexPath:(NSIndexPath*) indexPath
{
    if ([QXTUtility currentOsVersion] >= 7.0f)
    {
        [answerCell.answer removeFromSuperview];
        NSString *answerString = <your string>
        CGRect answerFrame = [answerString boundingRectWithSize:CGSizeMake(242.0f, 900.0f) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-Light" size:14.0f]} context:nil];
        answerFrame.origin.x = 20.0f;

        answerFrame.origin.y = answerCell.name.frame.origin.y + answerCell.name.frame.size.height + 6;
        answerFrame.size.height = ceilf(answerFrame.size.height);
        answerFrame.size.height += 30.0f;
        answerCell.answer.lineBreakMode = NSLineBreakByTruncatingTail;
        answerCell.answer.frame = answerFrame;

        UILabel *newLabel = [[UILabel alloc] initWithFrame:answerFrame];
        [newLabel setFont:[UIFont fontWithName:@"Roboto-Light" size:14.0f]];
        newLabel.text = answerString;
        newLabel.numberOfLines = 0;
        [answerCell.contentView addSubview:newLabel];
        [answerCell setNeedsDisplay];
    }
}