iOS 自定义表格视图单元格标签调整大小损坏

发布于 2024-11-24 23:03:06 字数 1500 浏览 3 评论 0原文

我正在为客户端编写一个 iOS 应用程序,主视图是一个表格视图,我用文本填充单元格,就像大多数 iOS twitter 客户端一样。

我创建了一个带有 3 个标签的自定义单元格视图,并用文本填充这些标签。其中一个标签需要根据文本量(最多 140 个字符)自动调整大小。

这工作正常,直到我向上/向下滚动列表,当文本延伸到单个整行(就像标签已扩展)或仅占用部分空间,因此它最终会更深并溢出到其他标签中(我确实有根据另一个类似的问题启用了“剪辑子视图”)

因此,当首次加载表格及其数据时,屏幕看起来像:

滚动之前 http://lloydsparkes.co.uk/files/iOS-BeforeScrolling.png

滚动一段时间后,它看起来像:

滚动后http://lloydsparkes.co.uk/files/iOS-AfterScrolling.png

所以看起来标签在滚动过程中正在调整大小(可能是由于重复使用相同的单元格?)并且没有调整回正确的大小尺寸。

我填充单元格的代码是:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *myID = @"customCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myID];

if(cell == NULL) {

    [[NSBundle mainBundle] loadNibNamed:@"CustomCellView" owner:self options:nil];

    cell = customCell;
    self.customCell = nil;
}

Post *p = [posts objectAtIndex:indexPath.row];

UILabel *label = (UILabel*)[cell viewWithTag:15];
label.text = p.Name;

label = (UILabel*)[cell viewWithTag:10];
label.text = p.Text;
[label sizeToFit];

label = (UILabel*)[cell viewWithTag:20];
label.text = [p Source];

//[cell layoutSubviews];

return cell;
}

那么我要问的是,是什么导致了这个问题?如何解决?

I am writing an iOS app for a client, the main view is a table view, and i fill the cells with text, much like most iOS twitter clients.

I have created a custom cell view with 3 labels, and i fill these labels with the texts. One of the labels needs to resize automatically depending on the amount of text there is (maximum of 140 characters).

This works fine until i scroll up/down the list when the text either extends to a single full line (like the label has expanded) OR only takes up part of the space so it ends up deeper and overflowing into other labels (i do have "Clip Subviews" enabled as per another similar SO question)

So when the table and its data is first loaded the screen looks like:

before scrolling http://lloydsparkes.co.uk/files/iOS-BeforeScrolling.png

And after a bit of scrolling it looks like:

after scrolling http://lloydsparkes.co.uk/files/iOS-AfterScrolling.png

So it seems that the label is resizing during the scrolling (maybe due to the same cell being reused?) and not resizing back to the correct size.

My code for filling a cell is:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *myID = @"customCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myID];

if(cell == NULL) {

    [[NSBundle mainBundle] loadNibNamed:@"CustomCellView" owner:self options:nil];

    cell = customCell;
    self.customCell = nil;
}

Post *p = [posts objectAtIndex:indexPath.row];

UILabel *label = (UILabel*)[cell viewWithTag:15];
label.text = p.Name;

label = (UILabel*)[cell viewWithTag:10];
label.text = p.Text;
[label sizeToFit];

label = (UILabel*)[cell viewWithTag:20];
label.text = [p Source];

//[cell layoutSubviews];

return cell;
}

So what i am asking is, what is causing this problem? and how can it be solved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏の忆 2024-12-01 23:03:06

sizeToFit 方法的行为可能会很奇怪,因为每次调用 cellForRowAtIndexPath 时 UILabel 框架都会发生变化。

您可以尝试的一件事是在调用 sizeToFit 之前将 label 对象的框架设置回原始大小(NIB 文件中设置的值)

[label setFrame:CGRectMake(0.0f, 0.0f, 100.0f, 50.0f)];    
[label sizeToFit];

The sizeToFit method might be behaving in a weird way because the UILabel frame is changed every time cellForRowAtIndexPath is called.

One thing you can try is setting the frame of the label object back to the original size (the valeu set in the NIB file) right before calling sizeToFit

[label setFrame:CGRectMake(0.0f, 0.0f, 100.0f, 50.0f)];    
[label sizeToFit];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文