0

我有一套 uitableviewcell 。我想为以下条件应用边框颜色为 firstcell 应用顶部边框颜色,为 lastcell 应用底部边框颜色。

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

4

2 回答 2

1

这是可能的。你要做的是,

  1. 创建一个自定义UITableView单元格,将其设置为您想要的样式。
  2. 在造型之后添加 2UIView的高度1或任何你想要的高度。让我们为它们命名topSeparator&bottomSeparator用于演示目的。
  3. 将一个标题约束到ContentView自定义表格视图单元格的顶部,将另一个约束到底部。
  4. 假设您是 using Storyboards,将topSeparator&都连接bottomSeparator到您的自定义单元格,
  5. 在or方法中disabletopSeparator&之后,取决于您是要以编程方式还是使用。bottomSeparatorinit(frame:)awakeFromNib()Nibs
  6. 在cell类中添加如下2个方法
// Unhides top s
func showTopSeparator() {
    self.topSeparator.isHidden = false
}

// Unhides bottom separator
func showBottomSeparator() {
    self.bottomSeparator.isHidden = false
}
  1. 在要显示单元格的视图控制器中,添加一个标志以显示基于IndexPath单元格的分隔符。见下文
func tableView(_ tableView: UITableView, 
  cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      // Dequeueing custom cell
      let cell = self.tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath as IndexPath) as! CustomCell
      
      // Flag to display separators
      if indexPath.row == 0 {
         cell.showTopSeparator()
      else if indexPath.row == data.count - 1 {
         cell.showBottomSeparator()
      }
      return cell

}
于 2021-07-11T10:48:17.117 回答
0

添加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, y: 0, 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, color: .blue, thickness: 0.5)
        }
        
        return cell
    }
}

在此处输入图像描述

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

在此处输入图像描述

在此处输入图像描述

于 2021-07-13T01:34:40.723 回答