Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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 如何以编程方式使UICollectionView填充UITableViewCell?_Ios_Swift_Uitableview_Uicollectionview - Fatal编程技术网

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

Ios 如何以编程方式使UICollectionView填充UITableViewCell?,ios,swift,uitableview,uicollectionview,Ios,Swift,Uitableview,Uicollectionview,我试着做一些相当简单的事情,但我总是出错 我有一个包含多个部分的UITableView。每个部分都有一个标题和一个UITableViewCell。每个单元格中都有一个UICollectionView。所有这些视图都必须以编程方式创建(我不能使用故事板解决方案) 问题是当我尝试调整UICollectionView的大小和位置时。我希望UICollectionView填充整个UITableViewCell,因此首先我尝试使用self.frame在UITableViewCell的init方法中调整UI

我试着做一些相当简单的事情,但我总是出错

我有一个包含多个部分的UITableView。每个部分都有一个标题和一个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 }
collectionView?.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
这给了我一个布局约束错误,并说“UICollectionViewProject[63851:2306772][LayoutConstraints]无法同时满足约束。”我不明白这怎么可能。我只添加了一个约束,故事板中没有任何内容,它可能会打破什么约束


不知何故,我需要使此UICollectionView的大小与UITableViewCell的大小相同,并完全适合它。

您为集合视图设置的大小将根据自动布局(从集合视图默认自动调整大小遮罩自动转换)被覆盖,因此您需要设置适当的约束

如果希望集合视图填满整个单元格,请尝试在
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:)
中告诉您的表视图代理您只在代码中添加了1个
约束
-
bottomAnchor
<代码>自动布局不是这样工作的。只需在添加
UITableViewCell
UICollectionView
作为
UITableViewCell
子视图后添加所有必需的
约束,即

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
}

谢谢你!我的原始代码中缺少的一段关键代码是translatesAutoresizingMaskIntoConstraints=false。如果不将该属性设置为false,约束将无法工作。当然..快乐编码..:)Ferschae,缺少的部分是collectionView?.TranslatesAutoResizezingMachinToConstraints=false。当我添加这段代码时,您的建议非常有效。请更新您的答案以包含该代码。谢谢,当然!默认的自动调整大小掩码约束与这些约束冲突!