xcode tableView 中的自定义分隔符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39544230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:15:52  来源:igfitidea点击:

Custom separator in tableView

iosswiftxcodeuitableviewseparator

提问by Oleg_D

I have my own separator in .jpg and would like to use it instead of default one in TableView, but the only thing I get is that it disappears.

我在 .jpg 中有自己的分隔符,并想在 TableView 中使用它而不是默认分隔符,但我唯一得到的是它消失了。

Swift 3.0:

斯威夫特 3.0:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "musicCell", for: indexPath) as! SongTableViewCell

    let color = UIColor(patternImage: UIImage(named:"Separator.jpg")!)
    let separator = tableView.separatorColor(color)

    tableView.tableFooterView = UIView(frame: .zero)
    tableView.backgroundView = UIImageView(image: UIImage(named: "bckgrd.jpg"))
    cell.backgroundColor = UIColor.clear

    //tableView.separatorStyle = UITableViewCellSeparatorStyle.singleLine
    //tableView.separatorColor = UIColor(patternImage: UIImage(named: "Separator.jpg"))

This is one of the ways I've tried to implement. Is there other way to do that?

这是我尝试实施的方法之一。有没有其他方法可以做到这一点?

回答by Ketan Parmar

You need to set separator style also not color only. Set separator styleto single lineand make sure that your Separator.jpgimage looks like single line and you are getting color by let color = UIColor(patternImage: UIImage(named:"Separator.jpg")!)this statement!

您还需要设置分隔符样式,而不仅仅是颜色。设置separator stylesingle line并确保您的Separator.jpg图像看起来像单行,并且您正在通过let color = UIColor(patternImage: UIImage(named:"Separator.jpg")!)此语句获取颜色!

you can set seperator style something like,

您可以设置分隔符样式,例如,

 tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine

P.S : If you are checking this on simulator then check it in full resolution (i.e. cmd + 1or in simulator window - > scale - > 100%), becasue sometime actually there is no problem but because of smaller scale you can't see it

PS:如果你在模拟器上检查这个,然后在全分辨率(即cmd + 1或在模拟器中window - > scale - > 100%)检查它,因为有时实际上没有问题,但由于规模较小,你看不到它

Update :

更新 :

Another way : Set SeparatorStyle to Nonefrom interface builder or by code like,

另一种方式:Set SeparatorStyle to None从界面构建器或通过类似的代码,

   tableView.separatorStyle = UITableViewCellSeparatorStyle.None

then you can add seperator image(imageview of height 1 pixel or 2 pixel) to cell at bottom in content view of cell by code or interface builder like,

然后您可以seperator image通过代码或界面构建器将(高度为 1 像素或 2 像素的图像视图)添加到单元格内容视图底部的单元格中,

   let seperatorImageView = UIImageView.init(image: UIImage.init(named: "yourseperatorimg.jpg"))
    seperatorImageView.frame = CGRectMake(0, cell.contentView.frame.size.height - 1.0,  cell.contentView.frame.size.width, 1)
    cell.contentView.addSubview(seperatorImageView)

回答by Rahul Mayani

Set separator color on viewDidLoadmethod...

viewDidLoad方法上设置分隔符颜色...

override func viewDidLoad() {

    super.viewDidLoad()
    let color = UIColor(patternImage: UIImage(named:"Separator.jpg")!)
    tableView.separatorColor = color
}

enter image description here

在此处输入图片说明

回答by Bista

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    let color = UIColor(patternImage: UIImage(named:"Separator.jpg")!)
    tblView.separatorColor = color
}