创建自定义UITableView分隔线

时间:2013-06-28 14:38:41

标签: ios uitableview uikit uiappearance

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

enter image description here

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

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

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

3 个答案:

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

答案 1 :(得分:9)

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

看起来不错!感谢你

答案 2 :(得分:1)

Swift 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)
    }
}