我有一个继承自 UITableViewCell 的单元格类。它有下一个灰线:

但是,如果我不设置单元格文本,我将看不到这些行:

那么这条线的原因是什么?

更新

这是我的自定义单元格的代码:

@implementation MDItemTableViewCell

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    NSString *fontName = selected ? self.selectedFontName : self.fontName;
    self.textLabel.font = [UIFont fontWithName:fontName size:self.fontSize];
}

@end

更新 2

这是 cellForRowAtIndexPath 的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *source = [(MDChoiceField *)self.field source];
    MDChoiceItem *item = source[(NSUInteger)indexPath.row];

    MDItemTableViewCell *cell = [(MDItemTableView *)self.fieldView cellForRowAtIndexPath:indexPath];

    cell.textLabel.numberOfLines = 0;

    NSString *string = NSLocalizedString(item.title, nil);
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]
            initWithString:string];
    [attributedString addAttribute:NSKernAttributeName
                             value:@(1.2)
                             range:NSMakeRange(0, [string length])];
    cell.textLabel.attributedText = attributedString;

    return cell;
}

并查看一:
- (MDItemTableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MDItemTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    cell.textLabel.font = [UIFont fontWithName:self.fontName size:self.fontSize];
    cell.fontSize = self.fontSize;
    cell.fontName = self.fontName;
    cell.selectedFontName = @"HelveticaNeue-Light";
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.textColor = [UIColor colorWithHexString:BLUE_COLOR];

    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }

    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds];
    cell.selectedBackgroundView.backgroundColor = [UIColor colorWithHexString:LIGHT_BLUE_COLOR];

    return cell;
}

抱歉有这么多行。

最佳答案

请注意,添加的行比表格单元格本身短。如果您在模拟器调试菜单中打开“颜色混合图层”,您可以看到它们似乎与标签的长度相同。这表明它是标签,而不是细胞,产生了奇怪的东西。

这个问题似乎相关:How can I remove UILabel's gray border on the right side?

这表明了以下修复,添加到 cellForRowAtIndexPath: :

[cell.textLabel setBackgroundColor:[UIColor clearColor]];

关于ios - UITableCell 查看顶部的可见行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27481654/

10-14 20:28