UILabel在iOS7中有一个奇怪的灰色顶线/边框,我该如何删除它?

And*_*ers 19 objective-c uitableview uilabel ios

我有一个基本的UILabel,我在一个UITableViewCell.我正在经历一些奇怪的行为,在一些细胞(不是全部)上,UILabel得到一个灰色的顶部边框,见下图.而且我不确定如何解决它.

在此输入图像描述

我像这样创建我的UILabel:

if (self.postText == nil) {
    CGRect postTextRect = CGRectMake(self.profileImage.frame.origin.x + self.profileImage.frame.size.width + 5, self.username.frame.origin.y + self.username.frame.size.height, frame.size.width - self.profileImage.frame.size.width - self.profileImage.frame.origin.y -10, self.frame.size.height - self.username.frame.size.height - self.username.frame.origin.y + 40);
    self.postText = [[UILabel alloc] initWithFrame:postTextRect];
    self.postText.backgroundColor = [UIColor whiteColor];
    self.postText.textColor = [UIColor darkGrayColor];
    self.postText.userInteractionEnabled = NO;
    self.postText.numberOfLines = 0;
    [self.containerView addSubview:self.postText];
}
Run Code Online (Sandbox Code Playgroud)

有想法该怎么解决这个吗?

更新

cellForRowAtIndexPath看起来像这样:

- (id)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [...]
    static NSString *postCellIdentifier = @"PostCell";
    PostCell *cell = [tableView dequeueReusableCellWithIdentifier:postCellIdentifier];
    if (cell == nil) {
        cell = [[PostCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:postCellIdentifier];
    }

    [self configureCell:cell atIndexPath:indexPath forPost:cellPost];
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

和这样的相关标准configureCell:

- (void)configureCell:(PostCell *)cell atIndexPath:(NSIndexPath *)indexPath forPost:(Post *)post
{
    [...]

    cell.username.text = cellUser.username;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    CGSize maximumLabelSize = CGSizeMake(cell.postText.frame.size.width, FLT_MAX);
    CGSize expectedLabelSize = [cell.postText.text sizeWithFont:cell.postText.font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
    CGRect newFrame = cell.postText.frame;
    newFrame.size.height = expectedLabelSize.height;
    cell.postText.frame = newFrame;

    CGRect containerRect = CGRectMake(5, 5, cell.containerView.frame.size.width, cell.postText.frame.size.height + cell.username.frame.origin.y + cell.username.frame.size.height + 10);
    if (containerRect.size.height < 65) {
        containerRect.size.height = 65;
    }

    cell.containerView.frame = containerRect;
    [...] 
}
Run Code Online (Sandbox Code Playgroud)

Bla*_*ago 39

我有同样的问题,并通过四舍五入解决它:

newFrame.size.height = ceilf(expectedLabelSize.height);
Run Code Online (Sandbox Code Playgroud)

  • 对于Swift:ceil(expectedLabelSize.height) (4认同)
  • 你也可以使用`cell.containerView.frame = CGRectIntegral(containerRect)`. (3认同)