【代码段】UITableView Section圆角

UITableView Section圆角

extension UITableView {
    /// section圆角 需要在 `WillDisplayCell`中调用
    /// - Parameters:
    ///   - cell: cell 对象
    ///   - indexPath: index
    ///   - cornerRadius: 圆角值
    ///   - inset: inset 偏移,比如 cell 含有 containerView
    /// - Important: 如果 Cell 有事件触发Cell高度改变,借助`tableView.beginUpdate`和`tableView.beginUpdate`来进行行高改变时,由于此次更新不走 `willDisplayCell`方法,可能导致Cell部分显示有问题(解决:使用 reload()或者不使用该方法)
    func sectionCornerWillDisplayCell(_ cell: UITableViewCell, forRowAt indexPath: IndexPath, cornerRadius: CGFloat, inset: UIEdgeInsets = .zero) {
        var pathRect = cell.bounds
        pathRect = pathRect.inset(by: inset)
        let numberOfRow = numberOfRows(inSection: indexPath.section)
        let headerView: UIView? = self.delegate?.tableView?(self, viewForHeaderInSection: indexPath.section)
        
        clearCellMask(cell)
        // 绘制曲线
        let row = indexPath.row
        let cornerSize = CGSize(width: cornerRadius, height: cornerRadius)
        var bezierPath: UIBezierPath?
        if headerView != nil {
            // 存在 header
            if row == 0, numberOfRow == 1 {
                // 一个 section 只有一行, 四边圆角
                bezierPath = UIBezierPath(roundedRect: pathRect, byRoundingCorners: [.allCorners], cornerRadii: cornerSize)
            } else if row == 0 {
                // 第一行 如果需要圆角由 header 处理,需要实现 willdisappearHeader 方法中做 layer 圆角
                bezierPath = UIBezierPath(roundedRect: pathRect, byRoundingCorners: [.topLeft, .topRight], cornerRadii: cornerSize)
            } else if row == numberOfRow-1 {
                // 组的最后一行
                bezierPath = UIBezierPath(roundedRect: pathRect, byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: cornerSize)
            } else {
                // 中间的行
                
                return
            }
        } else {
            if row == 0, numberOfRow == 1 {
                // 一个 section 只有一行, 四边圆角
                bezierPath = UIBezierPath(roundedRect: pathRect, byRoundingCorners: [.allCorners], cornerRadii: cornerSize)
            } else if row == 0 {
                bezierPath = UIBezierPath(roundedRect: pathRect, byRoundingCorners: [.topLeft, .topRight], cornerRadii: cornerSize)
            } else if row == numberOfRow-1 {
                // 组的最后一行
                bezierPath = UIBezierPath(roundedRect: pathRect, byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: cornerSize)
            } else {
                // 中间的行
                return
            }
        }
        guard let bezierPath = bezierPath else {
            return
        }
        let layer = CAShapeLayer()

        // MARK: - 后期实现边框阴影

        // 阴影颜色 #395b8e0c
//        layer.shadowColor = UIColor.red.cgColor
//        layer.shadowPath = bezierPath.cgPath
//        layer.shadowRadius = 5
        layer.path = bezierPath.cgPath
        cell.layer.mask = layer
    }
    
    private func clearCellMask(_ cell: UITableViewCell) {
        if let mask = cell.layer.mask, let shapeLayer = mask as? CAShapeLayer, shapeLayer.path != nil {
            cell.layer.mask = nil // 避免影响复用 cell
        }
    }
}

要在UITableView的section中添加数据,你需要先创建一个包含所需数据的数组。然后,在UITableViewDataSource协议中实现以下方法: 1. numberOfSections(in tableView: UITableView) -> Int:返回表格中的section数。 2. tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int:返回指定section中的行数。 3. tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell:返回指定indexPath的UITableViewCell实例。 例如,假设你有一个包含多个sectionUITableView,每个section都包含一个字符串数组。以下是一个示例代码: ``` class ViewController: UIViewController, UITableViewDataSource { var data: [[String]] = [["item 1", "item 2"], ["item 3", "item 4", "item 5"]] @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = self } // MARK: - UITableViewDataSource func numberOfSections(in tableView: UITableView) -> Int { return data.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return data[section].count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = data[indexPath.section][indexPath.row] return cell } } ``` 在这个例子中,我们创建了一个包含两个sectionUITableView。每个section都有一个字符串数组,我们将其存储在data数组中。在numberOfSections方法中,我们返回data数组的数量,即section的数量。在tableView(_:numberOfRowsInSection:)方法中,我们返回特定section中的行数。最后,在tableView(_:cellForRowAt:)方法中,我们获取特定indexPath的字符串并将其显示在UITableViewCell中。 注意,在上述示例代码中,我们将UITableViewCell标识符设置为“Cell”,你需要确保在Storyboard或xib文件中对应的UITableViewCell的标识符也设置为“Cell”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值