简体   繁体   English

UITableView分隔线

[英]UITableView Separator line

How can I change the separator line that appears at the end of each cell in UITableView? 如何更改UITableView中每个单元格末尾出现的分隔线? I want to have an image that is a thin separator type line image. 我想要一个图像是一个薄分隔符类型的线图像。

Set the separatorStyle of the tableview to UITableViewCellSeparatorStyleNone . 将tableview的separatorStyle设置为UITableViewCellSeparatorStyleNone Add your separator image as subview to each cell and set the frame properly. 将分隔符图像作为子视图添加到每个单元格并正确设置框架。

Try this 试试这个

Objective C 目标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")!)

First you can write the code: 首先你可以编写代码:

{    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];}

after that 之后

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

     }
}

Set the color of the separator to be patterned with your image. 使用图像设置要图案化的分隔符的颜色。

in viewDidLoad : viewDidLoad

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

My project is based on iOS 7 This helps me 我的项目基于iOS 7这对我有帮助

[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

Then put a subview into cell as separator! 然后将子视图放入单元格作为分隔符!

Try this: 试试这个:

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

That's an example of how I got it to work pretty well. 这是我如何让它工作得很好的一个例子。

Remember to set the separator style for the table view to none. 请记住将表视图的分隔符样式设置为none。

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

This is definitely help. 这绝对是有帮助的。 Working. 工作。 but set separator "none" from attribute inspector. 但是从属性检查器中设置分隔符“none”。 Write following code in cellForRowAtIndexPath method 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];

Swift 3/4 斯威夫特3/4

Custom separator line, put this code in a custom cell that's a subclass of UITableViewCell(or in CellForRow or WillDisplay TableViewDelegates for non custom cell): 自定义分隔符行,将此代码放在一个自定义单元格中,该单元格是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)

in viewDidLoad method: 在viewDidLoad方法中:

tableView.separatorStyle = .none

Here is an alternate way to add a custom separator line to a UITableView by making a CALayer for the image and using that as the separator line. 以下是通过为图像创建CALayer并将其用作分隔线,将自定义分隔线添加到UITableView的另一种方法。

// make a CALayer for the image for the separator line //为分隔线创建图像的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];

you can try below: 你可以尝试下面:

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];

or 要么

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

Simplest way to add a separator line under each tableview cell can be done in the storyboard itself. 在每个tableview单元格下添加分隔线的最简单方法可以在故事板本身中完成。 First select the tableview, then in the attribute inspector select the separator line property to be single line. 首先选择tableview,然后在属性检查器中选择分隔线属性为单行。 After this, select the separator inset to be custom and update the left inset to be 0 from the left. 在此之后,选择要自定义的分隔符插入,并将左侧插入从左侧更新为0。

Example

Here's the way to do this via storyboard in XCode 10.2.1. 这是通过XCode 10.2.1中的故事板实现此目的的方法。 Separator defaults to default which is the line. 分隔符默认为default ,即行。 Set it to none to remove the line. 将其设置为none以删除该行。

tableview_separator

You have 2 options to change the separator style of a uitableview if you want to change the default options which are no separators, solid line or etched line. 如果要更改没有分隔符,实线或蚀刻线的默认选项,您有2个选项可以更改uitableview的分隔符样式。

  1. The easiest consist in including a separator line background image to each cell view. 最简单的方法是在每个单元格视图中包含分隔线背景图像。 You may check then where is located your cell in the tableview to apply the right background image that will give you either a separator line on top of the cell or at the bottom of the cell. 您可以检查表格视图中单元格的位置,以应用右侧背景图像,该图像将在单元格顶部或单元格底部为您提供分隔线。

    Set the separator style to none in the viewDidLoad of your tableview: 在tableview的viewDidLoad中将分隔符样式设置为none:

    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

    Set your background image in the (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath function 在(UITableViewCell *)tableView中设置背景图像:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath函数

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

    cell.backgroundView = [[UIImageView alloc] initWithImage:yourBgImg]; cell.backgroundView = [[UIImageView alloc] initWithImage:yourBgImg];

    check the position of your cell in the section with the following: 使用以下内容检查单元格中的单元格位置:

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

  2. Add the separator line as a cell. 将分隔线添加为单元格。 I find a post recently for this here: http://www.dimzzy.com/blog/2012/01/separator-cells-for-uitableview/#disqus_thread 我最近在这里找到了一篇文章: http//www.dimzzy.com/blog/2012/01/separator-cells-for-uitableview/#disqus_thread

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM