首页 文章

如何以编程方式使UICollectionView填充UITableViewCell?

提问于
浏览
1

我正在尝试做一些相当简单的事情,但我一直都会遇到错误 .

我有一个包含多个部分的UITableView . 每个部分都有一个 Headers 和一个UITableViewCell . 每个单元格内都有一个UICollectionView . 所有这些视图都必须以编程方式创建(无论如何都没有故事板解决方案) .

问题是当我尝试调整UICollectionView的大小和位置时 . 我希望UICollectionView能够填充整个UITableViewCell,所以最初我尝试使用self.frame在UITableViewCell的init方法中调整UICollectionView的大小,如下所示:

var collectionView: UICollectionView?
let collectionViewLayout: UICollectionViewFlowLayout!

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

        self.collectionViewLayout = UICollectionViewFlowLayout()
        self.collectionViewLayout.sectionInset = UIEdgeInsets(top: 1.0, left: 1.0, bottom: 1.0, right: 1.0)
        self.collectionViewLayout.scrollDirection = .vertical

        super.init(style: style, reuseIdentifier: reuseIdentifier)

        let frame = CGRect(x: 0.0, y: 0.0, width: self.frame.width, height: self.frame.height)
        self.collectionView = UICollectionView(frame: frame, collectionViewLayout: self.collectionViewLayout)

        self.collectionView?.dataSource = self
        self.collectionView?.delegate = self
}

但是,我认为此时未设置UITableViewCell的帧(init方法调用),因此UICollectionView的大小不正确 . 那是对的吗?

接下来,我尝试使用布局锚点,如下所示:

collectionView?.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true

这给了我一个布局约束错误,并说'UICollectionViewProject [63851:2306772] [LayoutConstraints]无法同时满足约束 . 我不明白这是怎么可能的 . 我只添加了一个约束,故事板中没有任何内容,它可能会破坏哪些约束?

不知何故,我需要使这个UICollectionView与UITableViewCell的大小相同,并完美地适应它 . 同样,这必须以编程方式完成,不得使用故事板 . 请让我知道我需要做什么 . 谢谢 .

2 回答

  • 1

    在您的代码中,您只添加了1 constraint - bottomAnchor . Autolayout 不是这样的 . 添加 UICollectionView 作为 subviewUITableViewCell 后添加所有必需的 constraints ,即

    override init(style: UITableViewCellStyle, reuseIdentifier: String?)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    
        self.collectionViewLayout = UICollectionViewFlowLayout()
        self.collectionViewLayout.sectionInset = UIEdgeInsets(top: 1.0, left: 1.0, bottom: 1.0, right: 1.0)
        self.collectionViewLayout.scrollDirection = .vertical
    
        let frame = CGRect(x: 0.0, y: 0.0, width: self.frame.width, height: self.frame.height)
        self.collectionView = UICollectionView(frame: frame, collectionViewLayout: self.collectionViewLayout)
        self.collectionView?.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(self.collectionView!)
    
        NSLayoutConstraint.activate([
            self.collectionView!.topAnchor.constraint(equalTo: self.topAnchor),
            self.collectionView!.bottomAnchor.constraint(equalTo: self.bottomAnchor),
            self.collectionView!.leftAnchor.constraint(equalTo: self.leftAnchor),
            self.collectionView!.rightAnchor.constraint(equalTo: self.rightAnchor),
            ])
    
        self.collectionView?.dataSource = self
        self.collectionView?.delegate = self
    }
    
  • 2

    您为集合视图设置的大小将基于autolayout(从集合视图默认自动调整掩码自动翻译)覆盖,因此您需要设置适当的约束 .

    如果您希望集合视图填满整个单元格,请尝试在 init 方法的末尾添加以下代码:

    collectionView.translatesAutoresizingMaskIntoConstraints = false
    addSubview(collectionView)
    
    let layoutAttributes: [NSLayoutAttribute] = [.top, .bottom, .leading, .trailing]
    let constraints = layoutAttributes.map {
        NSLayoutConstraint(item: self, attribute: $0, relatedBy: .equal, toItem: collectionView, attribute: $0, multiplier: 1.0, constant: 0.0)
    }
    NSLayoutConstraint.activate(constraints)
    

    然后,您必须计算集合视图的高度,并在方法 tableView(_:, heightForRowAt:) 中告诉您的表视图委托 .

相关问题