UITableViewCell 重叠
我有一个 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;
}
但是,如果标签的大小太大,单元格会相互重叠。请问您能告诉我如何防止这种情况发生吗?
这是一张向您展示我的问题的图片:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否只是忘记在索引方法的行高度中考虑单元格的其余内容?您将向detailTextLabel 的高度添加11 个点。看来这还不够。运行 [@"Rights" sizeWithFont:[UIFont systemFontOfSize:15]] 返回高度 19。因此,您需要这么多来说明 textLabel 高度,加上 textLabel 和 textLabel 之间留下的任何空间。详细信息,加上单元格内的顶部和底部边距,以及单元格的任何其他内容。
另外,从您的屏幕截图来看,该特定单元格内的内容似乎可能有一些重叠。这可能是您为detailTextLabel 设置的框架的结果:
您将框架的原点设置为(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:
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.