iOS 自定义表格视图单元格标签调整大小损坏
我正在为客户端编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
sizeToFit
方法的行为可能会很奇怪,因为每次调用cellForRowAtIndexPath
时 UILabel 框架都会发生变化。您可以尝试的一件事是在调用
sizeToFit
之前将label
对象的框架设置回原始大小(NIB 文件中设置的值)The
sizeToFit
method might be behaving in a weird way because the UILabel frame is changed every timecellForRowAtIndexPath
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 callingsizeToFit