Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Collectionview动态宽度未工作_Ios_Swift - Fatal编程技术网

Ios Collectionview动态宽度未工作

Ios Collectionview动态宽度未工作,ios,swift,Ios,Swift,我在表视图中有一个collectionView,它工作得很好,但问题是collectionView单元格宽度在初始阶段不工作得很好,但在滚动它时工作得很好 您可以在这里检查,在第一节中,它显示全名,但在另一节中,它截断尾部,并在滚动之后工作 这里是重要的代码 class SubjectsViewFlowLayout: UICollectionViewFlowLayout { override func layoutAttributesForElements(in rect: CGRect)

我在表视图中有一个collectionView,它工作得很好,但问题是collectionView单元格宽度在初始阶段不工作得很好,但在滚动它时工作得很好

您可以在这里检查,在第一节中,它显示全名,但在另一节中,它截断尾部,并在滚动之后工作

这里是重要的代码

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()
}
}
类主题视图布局:UICollectionViewFlowLayout{
是否覆盖func layoutAttributesForElements(在rect:CGRect中)->[UICollectionViewLayoutAttributes]{
让attributesForElementsInRect=super.layouttributesforements(in:rect)
var newAttributesForElementsInRect=[UICollectionViewLayoutAttributes]()
//使用一个值来跟踪左边距
var leftMargin:CGFloat=0.0
对于attributesForElementsInRect中的属性{
让重构属性=属性
//为下一行赋值
if(refAttributes.frame.origin.x==self.sectionInset.left){
leftMargin=自剖插图左
}否则{
//将属性的x位置设置为当前边距
var newLeftAlignedFrame=refAttributes.frame
newLeftAlignedFrame.origin.x=左边距
如果newLeftAlignedFrame.origin.x+newLeftAlignedFrame.size.width>(self.collectionView?.bounds.size.width){
leftMargin=0.0
newLeftAlignedFrame.origin.x=0.0
if(newAttributesForElementsRect.last?.frame.origin.y==newLeftAlignedFrame.origin.y){
newLeftAlignedFrame.origin.y=newLeftAlignedFrame.origin.y+newLeftAlignedFrame.height+最小线间距
}
}
refAttributes.frame=newLeftAlignedFrame
}
//计算当前保证金的新值
leftMargin+=refAttributes.frame.size.width+10
newAttributesForElementsInRect.append(重构属性)
}
返回newAttributesForElementsRect
}
}
类DynamicCollectionView:UICollectionView{
覆盖func布局子视图(){
super.layoutSubviews()
if!\uu CGSizeEqualToSize(bounds.size、self.intrinsicContentSize){
self.invalidateIntrinsicContentSize()
如果self.superview?.superview?.superview是UITableView{
(self.superview?.superview?.superview作为!UITableView).beginUpdate()
(self.superview?.superview?.superview为!UITableView).endUpdates()
}
}
}
重写变量intrinsicContentSize:CGSize{
返回collectionViewLayout.collectionViewContentSize
}
}
类TagTableCell:UITableViewCell{
@ibvar collectionView:UICollectionView!
@IBOutlet弱var流量布局:主题查看流量布局!
重写func awakeFromNib(){
super.awakeFromNib()
register(UINib(nibName:“TagCollectionCell”,bundle:nil),forCellWithReuseIdentifier:“TagCollectionCell”)
}
func setupCell(){
让flowLayout=collectionView.collectionViewLayout作为?主题查看布局
flowLayout?.estimatedItemSize=.init(宽度:100,高度:45)
DispatchQueue.main.asyncAfter(截止日期:.now()+0.3){
self.collectionView.reloadData()
}
}
func setCollectionViewDataSourceDelegate(dataSourceDelegate:D,forRow row:Int){
setupCell()
collectionView.delegate=dataSourceDelegate
collectionView.dataSource=dataSourceDelegate
collectionView.tag=行+1
collectionView.layoutIfNeeded()的
}
}

只需添加此功能即可修改单元格大小,更改每个单元格所需的宽度和高度。 下面是一个使单元格宽度为集合视图宽度一半的示例

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

在tableview单元格中添加
UICollectionView
时,只需在异步方法中调用
datasource
datadelegate

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

有人把这个问题标记为离题。我想他甚至没有读过这个问题,或者根本不懂编程。