1

在 UITableViewCell 下使用 UICollectionView

使用以下扩展创建虚线边框:扩展 UIView {

    func addDashedLineView(frame: CGRect) {

        let border = CAShapeLayer()
        border.strokeColor = UIColor.blue.cgColor
        border.lineDashPattern = [6, 6]
        border.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)//self.frame
        border.fillColor = UIColor.clear.cgColor
        border.lineJoin = kCALineJoinRound
        //print("widht: \(frame.width) fraheight: \(UIScreen.main.bounds.width)")
        border.fillColor = nil
        border.path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: frame.width, height: frame.height), cornerRadius: 6).cgPath
        self.clipsToBounds = true

        self.layer.addSublayer(border)
    }
}

下面是我调用上述方法的集合视图方法(实际上单元格宽度取决于内容大小,所以我正在计算文本宽度)问题是当单元格从列表中出列时,边框也会从不同宽度重绘我正在使用自定义单元格“CollectionViewCell”类:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell


       let width = self.collectionData[indexPath.row].width(withConstraintedHeight: 14, font: .systemFont(ofSize: 17)) + Constant.cellPadding
        cell.contentView.addDashedLineView(frame: CGRect(x: 0, y: 0, width: width, height: Constant.itemHeight))

        cell.contentView.setNeedsLayout()
        cell.contentView.layoutIfNeeded()
        cell.layoutIfNeeded()
        return cell
    }

滚动视图时第一次正确加载绘图边框时已重绘如何解决此问题 在此处输入图像描述

4

0 回答 0