我在 .jpg 中有自己的分隔符,想在 TableView 中使用它而不是默认分隔符,但我唯一得到的是它消失了。
swift 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"))
这是我尝试实现的方法之一。还有其他方法吗?
最佳答案
您需要设置分隔符样式,而不仅仅是颜色。将 separator style
设置为 single line
并确保您的 Separator.jpg
图像看起来像单行并且您通过 let 获得颜色color = UIColor(patternImage: UIImage(named:"Separator.jpg")!)
这条语句!
你可以像这样设置分隔符样式,
tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
P.S :如果您在模拟器上检查它,那么在全分辨率下检查它(即 cmd + 1
或在模拟器 window -> scale -> 100%
中),因为有时实际上没有问题但是因为规模较小你看不到
更新:
另一种方法:Set SeparatorStyle to None
来自 interface builder 或通过类似的代码,
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
然后您可以通过代码或界面构建器将分隔符图像
(高度为 1 像素或 2 像素的 ImageView )添加到单元格底部的单元格,
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)
关于ios - tableView 中的自定义分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39544230/