【问题标题】:My cell's resize after scrolling upwards?向上滚动后我的单元格调整大小?
【发布时间】:2012-11-04 22:38:46
【问题描述】:

我的 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

【问题讨论】:

  • 常数 162 代表什么?看起来您正在尝试为所有三个标签设置相同的最大宽度。

标签: ios uitableview uiscrollview


【解决方案1】:

重用单元格时,应设置所有标签的框架。原因是 -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;
}

【讨论】:

  • 注意sizeToFit 调用超出了单元初始化块,这意味着也为重用的单元调用。
  • 我正在考虑以另一种方式实现我的 customcell,而不是我在上面的代码中所做的方式,因为我无法解决这个问题。你们有什么想法我可以用另一种方式实现吗? commentLabel 是重要的部分,正如您在图片上看到的那样,该标签的文本可以变化很大,其他标签在高度方面是静态的。感谢您迄今为止的帮助...
  • 感谢您的回答。但是您尝试设置框架的方式有问题,因为当我重用您的代码时,commentLabel 变为空白,commentLabel 中看不到任何文本。任何想法可能是什么? CGRect frame = commentLabel.frame; frame.size.width = kLabelWidth;框架.尺寸.高度 = 10000; commentLabel.frame = 框架;框架 = 评论标签.frame; frame.size.height += kOffset; commentLabel.frame = 框架;
  • 调用-sizeToFit方法后能否记录commentLabel的帧?你可以设置commentLabel的背景颜色来看看发生了什么
  • 检查我的编辑是否有 1 个commentLabels 输出.....并且commentLabel 的背景颜色设置为灰色,所以这不是问题。如果我删除框架代码,则文本将显示在单元格中
猜你喜欢
  • 1970-01-01
  • 2014-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多