UITableCellView顶部可见线条

时间:2014-12-15 10:10:53

标签: ios uitableview

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

但是,如果我没有设置单元格文本,我就看不到这些内容: screenshot2

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

更新

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

@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;
}

很抱歉这么多行。

5 个答案:

答案 0 :(得分:2)

一些设置可以帮助您,

  1. 作为Richa的回答,self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;它会隐藏表中的分隔符(默认值)。

  2. 当用户3781721回答时,tableName.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];将删除所有空单元格。所以表格会很简单。

  3. 在tableview的cellForRow数据源中,cell.clipsToBound = YES;返回单元格之前。

  4. 如果仍然没有解决,那么增加你的细胞高度很少,可能是5并检查。

答案 1 :(得分:2)

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

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

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

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

答案 2 :(得分:0)

将此代码放入ViewDidLoad

tableName.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

答案 3 :(得分:0)

使用' Grouped'样式UITableView将使表格不显示这些空白'细胞如果它们实际上是空的。你可以在初始化UITableView时声明这个,

UITableView *myTable = [[UITableView alloc] initWithStyle:UITableViewStyleGrouped];

或者如果您正在使用界面构建器,那么可以通过下拉菜单来更改“平原”的样式。到' Grouped'

答案 4 :(得分:0)

这些行是tableView的默认分隔线。只需将分隔符样式设置为NONE即可。在您声明UItableView的任何地方编写或使用此行。在Xib文件或代码中。

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

更新:1

- (void)viewDidLoad { [super viewDidLoad];
// This will remove extra separators from tableview
 self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
}

使用此功能,让我知道它是否有效。