gpt4 book ai didi

ios - 向上滚动后 UITableViewCell 高度变化?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:06:14 24 4
gpt4 key购买 nike

目前,在我的 tableView 中向上滚动时,我发现了一个奇怪而烦人的错误。当我决定释放“滚动条”时,这意味着我要放下“滚动条”,这样“ View ”将返回到显示所有 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];

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;

}

编辑

新代码:

CGRect frame = commentLabel.frame;
frame.size.width = kLabelWidth;
frame.size.height = 10000;
commentLabel.frame = frame;

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

但是我做错了什么,因为现在 commentLabel 中的文本没有正确显示,任何人都可以在我尝试设置框架时发现我的错吗?这就是细胞现在的样子 http://oi50.tinypic.com/2w5u0s6.jpg

新的完整代码:

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

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

CGRect frame = commentLabel.frame;
frame.size.width = kLabelWidth;
frame.size.height = 10000;
commentLabel.frame = frame;

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

commentLabel.numberOfLines = 0;
[commentLabel sizeToFit];

return cell;

}

最佳答案

您应该在重新使用单元格之前调整标签框。在您的情况下,您正在更改单元格高度,同时在

中重复使用它
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

但是你也应该根据单元格的高度调整标签的框架,这样它就不会超出单元格的边界。

关于ios - 向上滚动后 UITableViewCell 高度变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13227107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com