UITableView分隔线

时间:2011-01-26 12:36:54

标签: iphone uitableview

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

14 个答案:

答案 0 :(得分:88)

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

答案 1 :(得分:58)

试试这个

目标C

  [TableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];

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

<强>夫特

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

答案 2 :(得分: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;

     }
}

答案 3 :(得分:7)

使用图像设置要图案化的分隔符的颜色。

viewDidLoad中的

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

答案 4 :(得分:5)

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

[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

然后将子视图作为分隔符放入单元格中!

答案 5 :(得分:4)

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

答案 6 :(得分:4)

试试这个:

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

这是我如何让它运作良好的一个例子。

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

答案 7 :(得分: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];

答案 8 :(得分:1)

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

答案 9 :(得分:0)

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

  1. 最简单的方法是包括分隔线背景图像 到每个单元格视图。您可以检查您的手机所在的位置 tableview应用适合您的正确背景图像 要么是单元格顶部的分隔线,要么是底部的分隔线 细胞

    在您的viewDidLoad中将分隔符样式设置为none 的tableview:

      

    [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 alloc] 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

答案 10 :(得分:0)

通过为图片创建UITableView并将其用作分隔线,可以通过以下方式将自定义分隔线添加到CALayer

//为分隔线的图像制作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];

答案 11 :(得分: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;
}

答案 12 :(得分:0)

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

Example

答案 13 :(得分:0)

这是通过XCode 10.2.1中的情节提要进行此操作的方法。分隔符默认为default,即该行。将其设置为none以删除该行。

tableview_separator

相关问题