ios - tableView 中的自定义分隔符

标签 ios swift xcode uitableview separator

我在 .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/

相关文章:

ios - 是否可以手动设置/切换每个应用程序的 UserInterfaceStyle/暗模式?

ios - 是否可以仅使用 NSURLSession 为 UITableViews 构建异步图像下载机制?如何?

ios - 如何将 CGRect 值从钛发送到 ios 模块

swift - 表达式类型不明确,在 `ForEach` 中自定义类数组上没有更多上下文

ios - Xcode 9.3 和 Firebase Crashlytics 不工作

ios - 关于 swift 中的委托(delegate)和协议(protocol)的问题(举例)

c# - .Net 相当于 Swift 映射和归约

iOS Swift UI 测试(XCUITests)稳定性问题 - faSTLane 扫描

ios - 无法使用 CGRect 更改 CollectionView 框架

ios - 如何在 Swift 中的特定时间发送本地通知?