开发者

UITableViewCells are overlapping

开发者 https://www.devze.com 2023-02-27 05:50 出处:网络
I have a UITableViewCell which detailTextLabel causes the cells frame to be dependant on its size. My code for that is this:

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

My code for that is this:

c开发者_StackOverflow社区ell 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/


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号