Collectionview动态宽度不起作用Swift

时间:2019-01-30 08:01:36

标签: ios swift

我在Table视图中有一个collectionView,它工作正常,但问题是集合视图单元格宽度在初始阶段不能完美工作,但是一旦滚动它就可以工作。

https://docs.oracle.com/cd/E15523_01/apirefs.1111/e12419/tagdoc/af_query.html

您可以在此处检查是否在第一部分中显示了全名,但在其他部分中,它会截尾并在滚动后可以正常工作。

Here is the code that matters

class SubjectsViewFlowLayout: UICollectionViewFlowLayout {

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    let attributesForElementsInRect = super.layoutAttributesForElements(in: rect)
    var newAttributesForElementsInRect = [UICollectionViewLayoutAttributes]()
    // use a value to keep track of left margin
    var leftMargin: CGFloat = 0.0
    for attributes in attributesForElementsInRect! {
        let refAttributes = attributes
        // assign value if next row
        if (refAttributes.frame.origin.x == self.sectionInset.left) {
            leftMargin = self.sectionInset.left
        } else {
            // set x position of attributes to current margin
            var newLeftAlignedFrame = refAttributes.frame
            newLeftAlignedFrame.origin.x = leftMargin
            if newLeftAlignedFrame.origin.x + newLeftAlignedFrame.size.width > (self.collectionView?.bounds.size.width)! {
                leftMargin = 0.0
                newLeftAlignedFrame.origin.x = 0.0
                if (newAttributesForElementsInRect.last?.frame.origin.y == newLeftAlignedFrame.origin.y){
                    newLeftAlignedFrame.origin.y = newLeftAlignedFrame.origin.y + newLeftAlignedFrame.height + minimumLineSpacing
                }
            }
            refAttributes.frame = newLeftAlignedFrame
        }
        // calculate new value for current margin
        leftMargin += refAttributes.frame.size.width + 10
        newAttributesForElementsInRect.append(refAttributes)
    }
    return newAttributesForElementsInRect
}
}

class DynamicCollectionView: UICollectionView {
override func layoutSubviews() {
    super.layoutSubviews()
    if !__CGSizeEqualToSize(bounds.size, self.intrinsicContentSize) {
        self.invalidateIntrinsicContentSize()
        if self.superview?.superview?.superview is UITableView {
            (self.superview?.superview?.superview as! UITableView).beginUpdates()
            (self.superview?.superview?.superview as! UITableView).endUpdates()
        }
    }
}

override var intrinsicContentSize: CGSize {
    return collectionViewLayout.collectionViewContentSize
}
}

class TagTableCell: UITableViewCell {
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var flowLayout: SubjectsViewFlowLayout!

override func awakeFromNib() {
    super.awakeFromNib()
    collectionView.register(UINib(nibName: "TagCollectionCell", bundle: nil), forCellWithReuseIdentifier: "TagCollectionCell")
}

func setupCell() {
    let flowLayout = collectionView.collectionViewLayout as? SubjectsViewFlowLayout
    flowLayout?.estimatedItemSize = .init(width: 100, height: 45)

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
        self.collectionView.reloadData()
    }
}

func setCollectionViewDataSourceDelegate<D: UICollectionViewDataSource & UICollectionViewDelegate>(_ dataSourceDelegate: D, forRow row: Int) {
    setupCell()
    collectionView.delegate = dataSourceDelegate
    collectionView.dataSource = dataSourceDelegate
    collectionView.tag = row + 1
    collectionView.layoutIfNeeded()
}
}

2 个答案:

答案 0 :(得分:3)

您只需要在异步方法中调用datasourcedatadelegate,同时在表格单元格中添加UICollectionView

    DispatchQueue.main.async {
        self.collectionView.delegate = dataSourceDelegate
        self.collectionView.dataSource = dataSourceDelegate
    }

答案 1 :(得分:0)

您可以通过添加此功能来修改单元格大小,更改每个单元格所需的宽度和高度。 这是使单元格的宽度成为集合视图宽度的一半的示例。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let cellWidth = collectionView.layer.bounds.width / 2
        let cellHeight : CGFloat = 150
        return CGSize(width: cellWidth, height: cellHeight)
    }

您应该在您的课程中实现此协议:

UICollectionViewDelegateFlowLayout