【问题标题】:Create custom UITableView separator line创建自定义 UITableView 分隔线
【发布时间】:2013-06-28 14:38:41
【问题描述】:

我想创建一个像这样的分隔线:

关于如何实现它的任何想法? 我尝试获取线条的图像并使用UIAppearance 代理对象:

[[UITableView appearanceWhenContainedIn:[MyController class], nil] setSeparatorColor:
[UIColor colorWithPatternImage:[UIImage imageNamed:@"line.png"]]];
[[UITableView appearanceWhenContainedIn:[MyController class], nil] setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];

但是,不知何故,只有黑线被渲染。

【问题讨论】:

    标签: ios uitableview uikit uiappearance


    【解决方案1】:

    你可以试试下面的:

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

    【讨论】:

    • 我相信你的最后一行是:[cell.contentView addSubview:seperator];无论如何它不起作用,因为我需要一个有两种颜色的分隔线
    • 如何改为添加图片?
    • 我认为第二种方法效果更好,但我必须设置 CGRectMake(0, 1, 320, 1)
    • 奇怪的是我无法使用 UIAppearance 代理对象获得相同的效果
    • 我之前为我的一个应用程序做过这个技巧,它是一个静态 UITable,我为表格放置了一个预先设计的背景并删除了分隔符和单元格 bg ;)
    【解决方案2】:

    @Tarek 我使用您的对象的两个实例来创建双线

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

    看起来不错!为你点赞

    【讨论】:

      【解决方案3】:

      斯威夫特 3

      viewDidLoad:

      //remove default separator line
      tableView.separatorStyle = .none
      

      tableView 单元格:

      class MyCustomCell: UITableViewCell {
      
          override func awakeFromNib() {
              super.awakeFromNib()
      
              let separator = UIView(frame: CGRect(x: 8, y: bounds.size.height - 0.5, width: bounds.size.width - 22, height: 1))
              separator.backgroundColor = UIColor.red
              contentView.addSubview(separator)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-15
        • 2021-10-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多