简体   繁体   English

Collectionview动态宽度不起作用Swift

[英]Collectionview dynamic width not worked Swift

I have one collectionView inside Table view, it works perfectly but the issue is that collection view cell width not worked perfectly on initial stage but worked once scrolls it. 我在Table视图中有一个collectionView,它工作正常,但问题是集合视图单元格宽度在初始阶段不能完美工作,但一旦滚动它就可以工作。

检查这个

You can check here that in the first section, it shows full name but in other section, it truncates tail and that works after scrolls. 您可以在此处检查是否在第一部分中显示了全名,而在其他部分中,它会截尾并在滚动后可以使用。

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()
}
}

You just need to call datasource and datadelegate in async method while adding the UICollectionView in tableview cell 您只需要在异步方法中调用datasourcedatadelegate ,同时在tableview单元格中添加UICollectionView

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

You can modify your cell size just by adding this function, change your desired width and height for each cell. 您可以通过添加此功能来修改单元格大小,更改每个单元格所需的宽度和高度。 Here is an example to make the cell's width half of the collection view's. 这是使单元格的宽度成为集合视图宽度的一半的示例。

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)
    }

You should implement this Protocol to your class: 您应该对您的班级实施此协议:

UICollectionViewDelegateFlowLayout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift 3 中带有 CGSize 的 collectionView 的 100% 宽度 - 100% width of collectionView with CGSize in Swift 3 Swift 动态 CollectionView 高度:CollectionView 内 CollectionViewCell - Swift Dynamic CollectionView Height: CollectionView inside CollectionViewCell 在 iOS 中使用 Swift 3 的 CollectionView 动态高度 - CollectionView dynamic height with Swift 3 in iOS 如何水平居中将具有动态宽度的collectionView项目居中? - How to horizontally center collectionView items with dynamic width? collectionview单元格动态宽度中断布局 - collectionview cell dynamic width breaks layout 动态CollectionView取决于SWIFT 4.0中的TableViewCell值 - Dynamic CollectionView depends on TableViewCell Values in SWIFT 4.0 如何在Swift中显示3 * 3 CollectionView单元的动态高度 - How to Display 3*3 CollectionView Cell For Dynamic Height in Swift tableview单元格内部的Collectionview具有动态高度单元格和动态内容swift 4 - Collectionview inside tableview cell with dynamic height cells and dynamic content swift 4 如何在 Swift 中使用宽度约束将 Xib 加载到 CollectionView? - How to load Xib to CollectionView using width constraint in Swift? 加载Swift时CollectionView Cell更改其宽度和高度 - CollectionView Cell changes its width and height while loading Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM