【问题标题】:UITableView Separator lineUITableView 分隔线
【发布时间】:2011-06-15 20:00:31
【问题描述】:

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

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

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

    【讨论】:

    • 你能给我举个例子吗。
    • [separatorImageView setFrame:CGRectMake(0, 0, cell.frame.size.width, separatorImageView.image.size.height)];
    • [cell.imageView setFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];不能这样做:(
    • 当单元格没有填满屏幕时,普通 UITableView 绘制的那些呢?
    • 很遗憾,由于分隔线错误,这个答案仍然是最好的。
    【解决方案2】:

    试试这个

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

    【讨论】:

    • 真棒回答谢谢!
    • 谢谢@Sakshi - 我设置了分隔符颜色而不是图像,它看起来也很棒。 table.separatorStyle = UITableViewCellSeparatorStyle.SingleLine table.separatorColor = UIColor(red: 245/255, green: 245/255, blue: 245/255, alpha: 1.0)
    【解决方案3】:

    首先你可以写代码:

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

    【讨论】:

    • 这样的解决方案的问题是,只在tableView:cellForRowAtIndexPath: 的单元格中添加一个子视图,每次重用单元格时,它们都会添加一个新的子视图,而不会删除旧的子视图。它应该只添加一次,因此您可以在 UITableViewCell 子类中添加。
    【解决方案4】:

    设置要与您的图像搭配的分隔符颜色。

    viewDidLoad:

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

    【讨论】:

      【解决方案5】:

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

      [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

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

      【讨论】:

      • 这没有回答问题。
      【解决方案6】:

      试试这个:

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

      这是我如何让它工作得很好的一个例子。

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

      【讨论】:

        【解决方案7】:

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

        【讨论】:

        • 别忘了关闭表格视图的默认分隔符样式:[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        • 你能给我粗略的代码以便我开始,我怎样才能将它的原点设置为单元格的左下角?
        【解决方案8】:

        这是通过 XCode 10.2.1 中的故事板执行此操作的方法。分隔符默认为default,即行。将其设置为none 以删除该行。

        【讨论】:

          【解决方案9】:

          这绝对是有帮助的。在职的。 但从属性检查器中设置分隔符“无”。 在 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]; 也试过了,除了最右边和最左边之外,它几乎没有显示。
          【解决方案10】:

          斯威夫特 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
          

          【讨论】:

            【解决方案11】:

            如果您想更改无分隔符、实线或蚀刻线的默认选项,您有 2 个选项来更改 uitableview 的分隔符样式。

            1. 最简单的方法是包含分隔线背景图片 到每个单元格视图。您可以检查您的手机所在的位置 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 alloc] initWithImage:yourBgImg];

              使用以下内容检查单元格在该部分中的位置:

              NSInteger sectionRows = [tableView numberOfRowsInSection:[indexPathsection]]; NSInteger 行 = [索引路径行];

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

            【讨论】:

              【解决方案12】:

              你可以试试下面的:

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

              【讨论】:

                【解决方案13】:

                这是另一种将自定义分隔线添加到 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];
                

                【讨论】:

                  【解决方案14】:

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

                  Example

                  【讨论】:

                    猜你喜欢
                    • 2021-10-21
                    • 1970-01-01
                    • 2017-03-12
                    • 2014-11-19
                    • 1970-01-01
                    • 2018-02-07
                    • 2015-05-14
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多