UITableView分割线

81

我该如何更改在UITableView的每个单元格末尾显示的分隔线?我想要一个细分隔符类型的图像。

14个回答

95

将表格视图的separatorStyle设置为UITableViewCellSeparatorStyleNone。将分隔符图像添加为每个单元格的子视图,并正确设置其框架。


1
请给我一个例子。 - Mobile Developer iOS Android
[separatorImageView setFrame:CGRectMake(0, 0, cell.frame.size.width, separatorImageView.image.size.height)]; - Felix
[cell.imageView setFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)]; 我没能做到 :( - cV2
普通UITableView在单元格未填满屏幕时绘制的那些单元格呢? - Christian Schnorr
4
由于分隔线错误,这个回答仍然是最好的,这让人感到悲伤。 - eonil

65

试试这个

Objective C

  [TableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];

  [TableView setSeparatorColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Divider_line@2x.png"]]];

Swift

    tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
    tableView.separatorColor = UIColor(patternImage: UIImage(named: "YOUR_IMAGE_NAME")!)

1
非常棒的答案,谢谢! - Septronic
1
谢谢@Sakshi - 我设置了分隔符颜色而不是图像,效果也很好。table.separatorStyle = UITableViewCellSeparatorStyle.SingleLine table.separatorColor = UIColor(red: 245/255, green: 245/255, blue: 245/255, alpha: 1.0) - fs_tigre

30

首先,您可以编写代码:

{    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];}

之后

{    #define cellHeight 80 // You can change according to your req.<br>
     #define cellWidth 320 // You can change according to your req.<br>

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath
    {
         UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"seprater_line.png"]];
        imgView.frame = CGRectMake(0, cellHeight, cellWidth, 1);
        [customCell.contentView addSubview:imgView];
         return  customCell;

     }
}

4
像在 tableView:cellForRowAtIndexPath: 中只是向单元格添加子视图这样的解决方案的问题在于,它们会在每次单元格被重用时都添加一个新的子视图,而不会移除旧的子视图。应该仅添加一次,因此您应该在 UITableViewCell 子类中完成此操作。 - Gavin

7

将分隔符的颜色与您的图片进行图案化匹配。

viewDidLoad中:

self.tableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"mySeparatorImage"]];

5

我的项目基于iOS 7

这有助于我

[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

然后将一个子视图放入单元格作为分隔线!


1
那并没有回答问题。 - King-Wizard

4
尝试这个:
UIImageView *separator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"separator.png"]];
[cell.contentView addSubview: separator];

这是我成功实现的一个例子。

记得将表格视图的分隔符样式设置为无。


4
以下是关于如何在XCode 10.2.1中使用故事板的方法。分隔符默认为线条,可以将其设置为“none”以删除该线条。请注意保留HTML标签。

tableview_separator


4

您可以添加一个UIImageView,例如高度为1点,宽度与单元格框架相同,然后将其原点设置为单元格的左下角。


6
别忘了关闭表格视图的默认分隔线样式:[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; - Jasarien
你能给我提供大致的代码,这样我就可以开始编写了吗?如何将其原点设置为单元格的左下角? - Mobile Developer iOS Android

2

这绝对是有帮助的。工作正常。 但要从属性检查器中设置分隔符为 "none"。 在 cellForRowAtIndexPath 方法中编写以下代码

 UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,           
 cell.contentView.frame.size.height - 1.0,      
 cell.contentView.frame.size.width, 1)];

    lineView.backgroundColor = [UIColor blackColor];
    [cell.contentView addSubview:lineView];

我甚至尝试了[cell.contentView bringSubviewToFront:lineView];,但它大多数时候并没有显示,除了最左边和最右边的一些情况。 - Timothy Swan

2

Swift 3/4

自定义分割线,将以下代码放在自定义单元格中,该单元格是UITableViewCell的子类(或者对于非自定义单元格,在CellForRow或WillDisplay TableViewDelegates中):

let separatorLine = UIView.init(frame: CGRect(x: 8, y: 64, width: cell.frame.width - 16, height: 2))
separatorLine.backgroundColor = .blue
addSubview(separatorLine)

在viewDidLoad方法中:

tableView.separatorStyle = .none

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接