UITableViewCell 重叠

发布于 2024-11-02 14:38:49 字数 1919 浏览 1 评论 0原文

我有一个 UITableViewCell ,其中DetailTextLabel 导致单元格框架取决于其大小。

我的代码是这样的:

cell for row at index method

cell.textLabel.text = @"Rights";
            cell.backgroundColor = [UIColor clearColor];
            cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
            cell.detailTextLabel.numberOfLines = 0;
            cell.detailTextLabel.text = [self getItemForKey:kRights];
            cell.textLabel.font = [UIFont systemFontOfSize:15];
            cell.detailTextLabel.font = cell.textLabel.font;
            cell.textLabel.textColor = [UIColor colorWithRed:54.0f/255.0f green:54.0f/255.0f blue:54.0f/255.0f alpha:1.0f];
            CGSize constraintSize = CGSizeMake(310.0f, MAXFLOAT);
            CGSize labelSize = [[cell.detailTextLabel text] sizeWithFont:[cell.detailTextLabel font] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
            cell.detailTextLabel.frame = CGRectMake( 0, 0, 310, labelSize.height);
            cell.userInteractionEnabled = YES;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryNone;

Height for row at index method

 if (indexPath.row == 3){
                NSString *text = [self getItemForKey:kRights];
                CGSize constraintSize = CGSizeMake(310.0f, MAXFLOAT);
                CGSize labelSize = [text sizeWithFont:[UIFont systemFontOfSize:15]
                                    constrainedToSize:constraintSize 
                                        lineBreakMode:UILineBreakModeWordWrap];
                return labelSize.height+11;
            }

但是,如果标签的大小太大,单元格会相互重叠。请问您能告诉我如何防止这种情况发生吗?

这是一张向您展示我的问题的图片:

http://img689.imageshack.us/i/截图2011042102520.png/

I have a UITableViewCell which detailTextLabel causes the cells frame to be dependant on its size.

My code for that is this:

cell for row at index method

cell.textLabel.text = @"Rights";
            cell.backgroundColor = [UIColor clearColor];
            cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
            cell.detailTextLabel.numberOfLines = 0;
            cell.detailTextLabel.text = [self getItemForKey:kRights];
            cell.textLabel.font = [UIFont systemFontOfSize:15];
            cell.detailTextLabel.font = cell.textLabel.font;
            cell.textLabel.textColor = [UIColor colorWithRed:54.0f/255.0f green:54.0f/255.0f blue:54.0f/255.0f alpha:1.0f];
            CGSize constraintSize = CGSizeMake(310.0f, MAXFLOAT);
            CGSize labelSize = [[cell.detailTextLabel text] sizeWithFont:[cell.detailTextLabel font] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
            cell.detailTextLabel.frame = CGRectMake( 0, 0, 310, labelSize.height);
            cell.userInteractionEnabled = YES;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryNone;

Height for row at index method

 if (indexPath.row == 3){
                NSString *text = [self getItemForKey:kRights];
                CGSize constraintSize = CGSizeMake(310.0f, MAXFLOAT);
                CGSize labelSize = [text sizeWithFont:[UIFont systemFontOfSize:15]
                                    constrainedToSize:constraintSize 
                                        lineBreakMode:UILineBreakModeWordWrap];
                return labelSize.height+11;
            }

However, if the size of the label is too big, the cells overlap each other. Please could you tell me how I can prevent this?

Here is an image to show you my problem:

http://img689.imageshack.us/i/screenshot2011042102520.png/

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

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

发布评论

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

评论(1

浅浅淡淡 2024-11-09 14:38:49

您是否只是忘记在索引方法的行高度中考虑单元格的其余内容?您将向detailTextLabel 的高度添加11 个点。看来这还不够。运行 [@"Rights" sizeWithFont:[UIFont systemFontOfSize:15]] 返回高度 19。因此,您需要这么多来说明 textLabel 高度,加上 textLabel 和 textLabel 之间留下的任何空间。详细信息,加上单元格内的顶部和底部边距,以及单元格的任何其他内容。

另外,从您的屏幕截图来看,该特定单元格内的内容似乎可能有一些重叠。这可能是您为detailTextLabel 设置的框架的结果:

cell.detailTextLabel.frame = CGRectMake( 0, 0, 310, labelSize.height);

您将框架的原点设置为(0, 0),即单元格的左上角。这将使DetailTextLabel 与单元格的textLabel 重叠。

希望这有帮助。

Did you just forget, in the height for row at index method, to account for the rest of the content of the cell? You are adding 11 points to the height of the detailTextLabel. It doesn't look like that's enough. Running [@"Rights" sizeWithFont:[UIFont systemFontOfSize:15]] returns a height of 19. So, you would need that much to account for the textLabel height, plus any space you leave between textLabel & detail, plus top and bottom margins within the cell, plus any other content of the cell.

Also, from your screen shot, it seems like you might have some overlapping of the content within this particular cell. That could be a result of the frame you're setting for the detailTextLabel:

cell.detailTextLabel.frame = CGRectMake( 0, 0, 310, labelSize.height);

You set the origin of the frame at (0, 0), that is, the top left corner of the cell. That would make the detailTextLabel overlap the textLabel for the cell.

Hope this is helpful.

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