8

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

在此处输入图像描述

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

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

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

4

3 回答 3

9

你可以试试下面:

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;
}
于 2013-06-28T14:43:19.327 回答
8

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

看起来不错!为你点赞

于 2013-06-28T15:38:27.153 回答
1

斯威夫特 3

viewDidLoad:

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

表格视图单元格:

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)
    }
}
于 2017-04-13T13:01:30.883 回答