Ios 我的手机&x27;是否在向上滚动后调整大小?

Ios 我的手机&x27;是否在向上滚动后调整大小?,ios,uitableview,uiscrollview,Ios,Uitableview,Uiscrollview,我的TableViewCell布局有问题。目前,当我在tableView中向上滚动时,我发现了一个奇怪且恼人的bug。当我决定释放“卷轴”时,意味着我正在放下“卷轴”,这样“视图”将返回到显示所有TableView内容的正常位置,我的一些单元格会因为某种原因在宽度上重新调整大小。我不知道为什么会发生这种情况,也不知道问题出在哪里 我的所有手机都根据论坛中标签(commentLabel)的高度定制为fitSize。我想问题可能在于我如何定制手机内容。我会发布我的相关代码,也会发布到下面的图片 开始

我的TableViewCell布局有问题。目前,当我在tableView中向上滚动时,我发现了一个奇怪且恼人的bug。当我决定释放“卷轴”时,意味着我正在放下“卷轴”,这样“视图”将返回到显示所有TableView内容的正常位置,我的一些单元格会因为某种原因在宽度上重新调整大小。我不知道为什么会发生这种情况,也不知道问题出在哪里

我的所有手机都根据论坛中标签(commentLabel)的高度定制为fitSize。我想问题可能在于我如何定制手机内容。我会发布我的相关代码,也会发布到下面的图片

开始向上拖动滚动之前

释放/放下卷轴后,卷轴再次回到正常位置。现在其中一个单元格已更改

代码

- (UITableViewCell *)tableView:(UITableView *)pTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ForumthreadCell";
    UITableViewCell *cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Feedback *item = [self.items objectAtIndex:indexPath.row];

    UILabel *aliasLabel = (UILabel *)[cell viewWithTag:1];
    UILabel *commentLabel = (UILabel *)[cell viewWithTag:2];
    UILabel *dateLabel = (UILabel *)[cell viewWithTag:3];

    [aliasLabel setText:item.alias];
    [commentLabel setText:item.comment];
    [dateLabel setText:[self.dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:(double)item.time]]];

    commentLabel.numberOfLines = 0;
    [commentLabel sizeToFit];
    [aliasLabel sizeToFit];
    [dateLabel sizeToFit];

    return cell;
}

-(CGFloat)getLabelHeightForText:(NSString *)text andWidth:(CGFloat)labelWidth
{

    CGSize maximumSize = CGSizeMake(labelWidth, 10000);

    //provide appropriate font and font size
    CGSize labelHeighSize = [text sizeWithFont: [UIFont fontWithName:@"Trebuchet MS" size:12.0f]
                         constrainedToSize:maximumSize
                             lineBreakMode:UILineBreakModeTailTruncation];
    return labelHeighSize.height;
 }

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Feedback *item = [self.items objectAtIndex:indexPath.row];

    CGFloat commentTextHeight = [self getLabelHeightForText:item.comment andWidth:162];
    return commentTextHeight + 39;
}
编辑

NSLog(@"bounds.origin.x: %f", commentLabel.bounds.origin.x);
NSLog(@"bounds.origin.y: %f", commentLabel.bounds.origin.y);
NSLog(@"bounds.size.width: %f", commentLabel.bounds.size.width);
NSLog(@"bounds.size.height: %f", commentLabel.bounds.size.height);

NSLog(@"frame.origin.x: %f", commentLabel.frame.origin.x);
NSLog(@"frame.origin.y: %f", commentLabel.frame.origin.y);
NSLog(@"frame.size.width: %f", commentLabel.frame.size.width);
NSLog(@"frame.size.height: %f", commentLabel.frame.size.height);
为1个单元格生成此输出(1个标签输出)


重复使用单元格时,应设置所有标签的边框。原因是-sizeToFit将更改标签的框架,因此当您重用单元格时,标签的框架已经更改

编辑

NSLog(@"bounds.origin.x: %f", commentLabel.bounds.origin.x);
NSLog(@"bounds.origin.y: %f", commentLabel.bounds.origin.y);
NSLog(@"bounds.size.width: %f", commentLabel.bounds.size.width);
NSLog(@"bounds.size.height: %f", commentLabel.bounds.size.height);

NSLog(@"frame.origin.x: %f", commentLabel.frame.origin.x);
NSLog(@"frame.origin.y: %f", commentLabel.frame.origin.y);
NSLog(@"frame.size.width: %f", commentLabel.frame.size.width);
NSLog(@"frame.size.height: %f", commentLabel.frame.size.height);
您可以这样做:

static const CGFloat kLabelWidth = 162;
static const CGFloat kOffset = 39;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    UILabel *commentLabel = (UILabel *)[cell.contentView viewWithTag:2];
    CGRect frame = commentLabel.frame;
    frame.size.width = kLabelWidth;
    frame.size.height = 10000;
    commentLabel.frame = frame;
    commentLabel.text = @"your text";
    [commentLabel sizeToFit];

    frame = commentLabel.frame;
    frame.size.height += kOffset;
    commentLabel.frame = frame;

    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  {
    CGFloat commentTextHeight = [self getLabelHeightForText:@"your text" andWidth:kLabelWidth];
    return commentTextHeight + kOffset;
}

常数162代表什么?看起来您正试图为所有三个标签设置相同的最大宽度。请注意,
sizeToFit
调用不在单元初始化块中,这意味着也会为重用的单元调用。我正在考虑以另一种方式实现我的customcell,而不是以我在上述代码中的方式,因为我无法解决此问题。你们知道我如何用另一种方式实现吗?commentLabel是一个重要的部分,正如您在图片上看到的那样,该标签的文本可能变化很大,其他标签在高度方面是静态的。谢谢你迄今为止的帮助…谢谢你的回答。但是,您尝试设置框架的方式有问题,因为当我重用代码时,我的commentLabel变为空白,commentLabel中没有可见的文本。你知道那可能是什么吗?CGRect frame=commentLabel.frame;frame.size.width=kLabelWidth;frame.size.height=10000;commentLabel.frame=框架;frame=commentLabel.frame;frame.size.height+=kOffset;commentLabel.frame=框架;调用方法-sizeToFit后,是否可以记录commentLabel的框架?您可以设置commentLabel的背景色,以查看“检查我对1 commentLabels的编辑”输出时的情况。。。。。标签的背景色设置为灰色,所以这不是问题所在。如果删除框架代码,文本将显示在单元格中