可视图单元边框

问题描述

我有一套 uitableviewcell 。 我想为以下条件应用边框颜色 为 firstcell 应用顶部边框颜色 最后一个单元格的底部边框颜色。

我是 swift 的新手,所以我不确定这是否可行。

解决方法

这是可以做到的。你要做的是,

  1. 创建一个自定义的 UITableView 单元格,将其设置为您想要的样式。
  2. 样式后添加 2 个 UIView,高度为 1 或您想要的任何高度。出于演示目的,让我们将它们命名为 topSeparatorbottomSeparator
  3. 将其中一个标题限制在自定义 tableview 单元格 ContentView 的顶部,将另一个标题限制在底部。
  4. 假设您使用 Storyboards,将 topSeparatorbottomSeparator 连接到您的自定义单元格,
  5. disabletopSeparator 之后的 bottomSeparatorinit(frame:) 方法取决于您是要以编程方式还是使用 {{ 1}}。
  6. 在cell类中添加如下2个方法
awakeFromNib()
  1. 在要显示单元格的视图控制器中,添加一个标志以显示基于单元格的 Nibs 的分隔符。见下文
// Unhides top s
func showTopSeparator() {
    self.topSeparator.isHidden = false
}

// Unhides bottom separator
func showBottomSeparator() {
    self.bottomSeparator.isHidden = false
}
,

添加 Extension
您可以将下面的代码添加到您的 ViewController 文件中,也可以像我一样创建一个单独的文件。

CALayer+Extension.swift

import UIKit

extension CALayer {

    func addBorder(edge: UIRectEdge,color: UIColor,thickness: CGFloat) {
        
        let border = CALayer();
        
        switch edge {
        case UIRectEdge.top:
            border.frame = CGRect(x: 0,y: 0,width: self.frame.width,height: thickness)
            break
        case UIRectEdge.bottom:
            border.frame = CGRect(x:0,y:self.frame.height - thickness,width:self.frame.width,height:thickness)
            break
        case UIRectEdge.left:
            border.frame = CGRect(x:0,y:0,width: thickness,height: self.frame.height)
            break
        case UIRectEdge.right:
            border.frame = CGRect(x:self.frame.width - thickness,height:self.frame.height)
            break
        default:
            break
        }
        
        border.backgroundColor = color.cgColor;
        
        self.addSublayer(border)
    }
}

TableViewController.swift

import UIKit

class TableViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    let arr = ["a","b","c","d"]
    
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
        
        tableView.tableFooterView = UIView(frame: .zero)
    }
    
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return arr.count
    }
    
    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as? TableViewCell else { return UITableViewCell() }
        
        cell.label.text = arr[indexPath.row]
        
        //Setting the border
        if indexPath.row == 0 { //first cell
            cell.layer.addBorder(edge: .top,color: .blue,thickness: 0.5)
            
        }
        
        if indexPath.row == arr.count - 1 { //last cell
            cell.layer.addBorder(edge: .bottom,thickness: 0.5)
        }
        
        return cell
    }
}

enter image description here

如果要删除除第一个和最后一个单元格之外的边框,可以将此属性更改为 none

enter image description here

enter image description here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...