0

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

我的所有单元格都根据我论坛中标签(commentLabel)的高度定制为 fitSize。我认为问题可能在于我如何尝试自定义单元格的内容。我将发布我的相关代码并发布到下面的图片中。

在开始向上拖动滚动之前http ://tinypic.com/view.php?pic=2rfsum9&s=6

释放/放下卷轴后,再次回到正常位置。现在其中一个单元格发生了变化http ://tinypic.com/view.php?pic=swxnqv&s=6

代码

- (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 个 commentLabel 输出)

purgeIdleCellConnections: found one to purge conn = 0x1dd654f0
 bounds.origin.x: 0.000000
 bounds.origin.y: 0.000000
 bounds.size.width: 162.000000
 bounds.size.height: 10039.000000
 frame.origin.x: 12.000000
 frame.origin.y: 28.000000
 frame.size.width: 162.000000
 frame.size.height: 10039.000000
4

1 回答 1

0

重复使用单元格时,应设置所有标签的框架。原因是 -sizeToFit 会改变标签的框架,所以当你重用单元格时,标签的框架已经改变了。

编辑

你可以这样做:

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;
}
于 2012-11-05T03:09:32.393 回答