2011-01-26 101 views

回答

4

您可以添加一个UIImageView,例如,单元格的高度和宽度一样宽,然后将其原点设置为单元格的左下角。

+6

不要忘记关闭默认的分隔样式表视图:`[自我。tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];` – Jasarien 2011-01-26 12:41:04

+0

你可以给我粗略的代码,所以我可以开始,我如何设置它的起源单元格的左下角? – 2011-01-26 12:43:44

+1

我已经得到了解决方案,谢谢大家。 – 2011-01-26 13:25:32

84

将tableview的separatorStyle设置为UITableViewCellSeparatorStyleNone。将您的分隔符图像作为子视图添加到每个单元格并正确设置框架。

+1

你会plz给我一个例子。 – 2011-01-26 12:46:18

+0

[separatorImageView setFrame:CGRectMake(0,0,cell.frame.size.width,separatorImageView.image.size.height)]; – Felix 2011-01-26 12:54:43

+0

[cell.imageView setFrame:CGRectMake(0,0,cell.frame.size.width,cell.frame.size.height)];没有能够做到这一点:( – cV2 2011-03-16 14:40:33

0

如果要更改默认选项(无分隔符,实线或蚀刻线),您有2个选项可更改uitableview的分隔符样式。

  1. 最简单的由在包括分隔线背景图像 给每个小区视图。你可以检查你的单元格位于 tableview的哪个位置,以便应用正确的背景图像,该背景图像将为您提供 单元格顶部的分隔线或 单元格的底部。

    设置分隔符的风格没有你 的tableview的viewDidLoad中:

    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]。

    设置你的背景图像中(的UITableViewCell *)的tableView:(UITableView的*)的tableView 的cellForRowAtIndexPath:(NSIndexPath *)indexPath功能

    UIImage* yourBgImg = [[UIImage imageNamed:@"bgImage.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)]; 
    

    cell.backgroundView = [[UIImageView的页头] initWithImage:yourBgImg];

    检查你的细胞的结合下面的部分中的位置:

    NSInteger的sectionRows = [的tableView numberOfRowsInSection:[indexPathsection]]; NSInteger row = [indexPath row];

  2. 将分隔线添加为单元格。我发现一个帖子最近这 这里: http://www.dimzzy.com/blog/2012/01/separator-cells-for-uitableview/#disqus_thread
4

试试这个:

UIImageView *separator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"separator.png"]]; 
[cell.contentView addSubview: separator]; 

这就是我如何得到它工作得很好的例子。

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

29

首先,你可以写代码:

{ [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; 

    } 
} 
51

试试这个

目标C

[TableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine]; 

    [TableView setSeparatorColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"[email protected]"]]]; 

斯威夫特

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

设置分隔符的颜色与你的图像进行构图。

viewDidLoad

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

这里是通过使CALayer用于图像,并使用该添加自定义分隔线到UITableView的另一种方法为分隔线。

//作出的CALayer为分隔线的图像

CALayer *separator = [CALayer layer]; 
separator.contents = (id)[UIImage imageNamed:@"myImage.png"].CGImage; 
separator.frame = CGRectMake(0, 54, self.view.frame.size.width, 2); 
[cell.layer addSublayer:separator]; 
5

我的项目是基于iOS上的7 这有助于我

[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

然后把一个子视图到小区分隔器!

0

你可以试试下面:

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"separator.png"]]; 
    imageView.frame = CGRectMake(0, 100, 320, 1); 
    [customCell.contentView addSubview:imageView]; 

    return customCell; 
} 
2

这是绝对有帮助。加工。 ,但从属性检查器中设置分隔符“无”。在的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]; 
1

斯威夫特3/4

自定义的分隔线,把这个代码在这的UITableViewCell的子类中的自定义单元格(或CellForRow或WillDisplay TableViewDelegates为非定制单元格):

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

在每个tableview单元格下添加分隔线的最简单方法可以在t他故事板本身。 首先选择tableview,然后在属性检查器中选择分隔线属性为单行。之后,选择要自定义的分隔符插入,并将左侧插入从左侧更新为0。

Example