Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.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初始化类时多次调用UICollectionViewFlowLayout,为什么不能正确绘制?_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios 为什么在使用UICollectionView初始化类时多次调用UICollectionViewFlowLayout,为什么不能正确绘制?

Ios 为什么在使用UICollectionView初始化类时多次调用UICollectionViewFlowLayout,为什么不能正确绘制?,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我正在尝试开发一个UICollectionViewFlowLayout子类,它像UITableView一样布局项目。项目使用自动布局,我还支持补充标题视图 open override func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { guard let attribu

我正在尝试开发一个UICollectionViewFlowLayout子类,它像UITableView一样布局项目。项目使用自动布局,我还支持补充标题视图

open override func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    guard let attributes = super.layoutAttributesForSupplementaryView(ofKind: elementKind, at: indexPath) else { return nil }
    print("Section Attributes: \(attributes)")
    return attributes
}

override open func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    let attributes = super.layoutAttributesForItem(at: indexPath)
    if let attributes = attributes {
        guard let collectionView = collectionView else { return attributes }
        attributes.bounds.size.width = collectionView.bounds.width - sectionInset.left - sectionInset.right
    }
    return attributes
}

override open func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]?
{
    print("\nlayoutAttributesForElements: \(rect)")

    let allAttributes = super.layoutAttributesForElements(in: rect)
    return allAttributes?.flatMap { attributes in
        switch attributes.representedElementCategory {
        case .cell:
            print("\ncell: \(attributes.indexPath)");
            print("cell_old Attributes: \(attributes)")
            let a = layoutAttributesForItem(at: attributes.indexPath)
            print("cell_new Attributes: \(a)")
            return a
        case .supplementaryView:
            print("\nsupp: \(attributes.indexPath)");
            print("supp_old Attributes: \(attributes)")
            let a = layoutAttributesForSupplementaryView(ofKind: UICollectionElementKindSectionHeader, at: attributes.indexPath)
            print("supp_new Attributes: \(a)")
            return a
        default: return attributes
        }
    }
}
此外,my UICollectionViewCell子类会覆盖PreferredLayoutAttribute拟合:

override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes
{
    layoutAttributes.bounds.size.height = systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
    return layoutAttributes
}
这……有点效果。项目布局正确,但剖面标头视图在错误的y坐标处绘制。我可以通过在视图的控制器中调用
collectionView.reloadData()
来破解它,但这显然需要加载所有内容两次,这是错误的

调试输出显示,在调用
reloadData()
之前,layoutAttributesForElements会被调用三次:

layouttributesforements:(0.0,0.0375.0667.0)
单元格:[0,0]
单元格旧属性:索引路径:({length=2,path=0-0});帧=(6 56;50);
单元新属性:可选(索引路径:({length=2,path=0-0});帧=(-150.556;36350);)
单元格:[0,1]
单元格旧属性:索引路径:({length=2,path=0-1});帧=(68.56;50);
单元新属性:可选(索引路径:({length=2,path=0-1});帧=(-8856;36350);)
单元格:[0,2]
单元格旧属性:索引路径:({length=2,path=0-2});帧=(13156;50);
单元新属性:可选(索引路径:({length=2,path=0-2});帧=(-25.556;36350);)
单元格:[1,0]
单元格旧属性:索引路径:({length=2,path=1-0});帧=(6178;50);
单元新属性:可选(索引路径:({length=2,path=1-0});帧=(-150.5178;36350);)
单元格:[1,1]
单元格旧属性:索引路径:({length=2,path=1-1});帧=(68.5178;50);
单元新属性:可选(索引路径:({length=2,path=1-1});帧=(-88178;36350);)
单元格:[1,2]
单元格旧属性:索引路径:({length=2,path=1-2});帧=(131178;50);
单元格新属性:可选(索引路径:({length=2,path=1-2});帧=(-25.5178;36350);)
补充资料:[0,0]
支持旧属性:索引路径:({length=2,path=0-0});元素种类:(UICollectionElementKindSectionHeader);帧=(0;375 40);zIndex=10;
节属性:索引路径:({length=2,path=0-0});元素种类:(UICollectionElementKindSectionHeader);帧=(0;375 40);zIndex=10;
支持新属性:可选(索引路径:({length=2,path=0-0});元素种类:(UICollectionElementKindSectionHeader);框架=(0 0;375 40);zIndex=10;)
补充资料:[1,0]
支持旧属性:索引路径:({length=2,path=1-0});元素种类:(UICollectionElementKindSectionHeader);帧=(0 122;375 40);zIndex=10;
节属性:索引路径:({length=2,path=1-0});元素种类:(UICollectionElementKindSectionHeader);帧=(0 122;375 40);zIndex=10;
支持新属性:可选(索引路径:({length=2,path=1-0});元素种类:(UICollectionElementKindSectionHeader);框架=(0 122;375 40);zIndex=10;)
布局要素属性:(-667.0,-667.01042.01334.0)
单元格:[0,0]
单元格旧属性:索引路径:({length=2,path=0-0});帧=(6 56;50);
单元新属性:可选(索引路径:({length=2,path=0-0});帧=(-150.556;36350);)
单元格:[0,1]
单元格旧属性:索引路径:({length=2,path=0-1});帧=(68.56;50);
单元新属性:可选(索引路径:({length=2,path=0-1});帧=(-8856;36350);)
单元格:[0,2]
单元格旧属性:索引路径:({length=2,path=0-2});帧=(13156;50);
单元新属性:可选(索引路径:({length=2,path=0-2});帧=(-25.556;36350);)
单元格:[1,0]
单元格旧属性:索引路径:({length=2,path=1-0});帧=(6178;50);
单元新属性:可选(索引路径:({length=2,path=1-0});帧=(-150.5178;36350);)
单元格:[1,1]
单元格旧属性:索引路径:({length=2,path=1-1});帧=(68.5178;50);
单元新属性:可选(索引路径:({length=2,path=1-1});帧=(-88178;36350);)
单元格:[1,2]
单元格旧属性:索引路径:({length=2,path=1-2});帧=(131178;50);
单元格新属性:可选(索引路径:({length=2,path=1-2});帧=(-25.5178;36350);)
补充资料:[0,0]
支持旧属性:索引路径:({length=2,path=0-0});元素种类:(UICollectionElementKindSectionHeader);帧=(0;375 40);zIndex=10;
节属性:索引路径:({length=2,path=0-0});元素种类:(UICollectionElementKindSectionHeader);帧=(0;375 40);zIndex=10;
支持新属性:可选(索引路径:({length=2,path=0-0});元素种类:(UICollectionElementKindSectionHeader);框架=(0 0;375 40);zIndex=10;)
补充资料:[1,0]
支持旧属性:索引路径:({length=2,path=1-0});元素种类:(UICollectionElementKindSectionHeader);帧=(0 122;375 40);zIndex=10;
节属性:索引路径:({length=2,path=1-0});元素种类:(UICollectionElementKindSectionHeader);帧=(0 122;375 40);zIndex=10;
支持新属性:可选(索引路径:({length=2,path=1-0});元素种类:(UICollectionElementKindSectionHeader);框架=(0 122;375 40);zIndex=10;)
布局要素属性:(-667.0,-667.01042.01334.0)
单元格:[0,0]
单元格旧属性:索引路径:({length=2,path=0-0});帧=(6 56;363 45);
cell_新属性:可选(索引路径:({length=2,path=0-0});frame=(6 56;363 45);)
单元格:[0,1]
单元格旧属性:索引路径:({length=2,path=0-1});帧=(6107;36345);
单元格_新属性:可选(索引路径:({length=2,path=0-1});帧=(6107;36345);)
单元格:[0,2]
单元格旧属性:索引路径:({length=2,path=0-2});帧=(6158;36345);
layoutAttributesForElements: (0.0, 0.0, 375.0, 667.0)

cell: [0, 0]
cell_old Attributes: <UICollectionViewLayoutAttributes: 0x6180001fa100> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); frame = (6 56; 50 50); 
cell_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6000001fd000> index path: (<NSIndexPath: 0x60000023de40> {length = 2, path = 0 - 0}); frame = (-150.5 56; 363 50); )

cell: [0, 1]
cell_old Attributes: <UICollectionViewLayoutAttributes: 0x6180001fac00> index path: (<NSIndexPath: 0xc000000000200016> {length = 2, path = 0 - 1}); frame = (68.5 56; 50 50); 
cell_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6000001fb200> index path: (<NSIndexPath: 0x60000023de20> {length = 2, path = 0 - 1}); frame = (-88 56; 363 50); )

cell: [0, 2]
cell_old Attributes: <UICollectionViewLayoutAttributes: 0x6180001fa400> index path: (<NSIndexPath: 0xc000000000400016> {length = 2, path = 0 - 2}); frame = (131 56; 50 50); 
cell_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6000001fd900> index path: (<NSIndexPath: 0x60000023dde0> {length = 2, path = 0 - 2}); frame = (-25.5 56; 363 50); )

cell: [1, 0]
cell_old Attributes: <UICollectionViewLayoutAttributes: 0x6180001fa900> index path: (<NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}); frame = (6 178; 50 50); 
cell_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6000001fd800> index path: (<NSIndexPath: 0x60000023de00> {length = 2, path = 1 - 0}); frame = (-150.5 178; 363 50); )

cell: [1, 1]
cell_old Attributes: <UICollectionViewLayoutAttributes: 0x6180001faf00> index path: (<NSIndexPath: 0xc000000000200116> {length = 2, path = 1 - 1}); frame = (68.5 178; 50 50); 
cell_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6000001fdb00> index path: (<NSIndexPath: 0x60000023dcc0> {length = 2, path = 1 - 1}); frame = (-88 178; 363 50); )

cell: [1, 2]
cell_old Attributes: <UICollectionViewLayoutAttributes: 0x6180001fb600> index path: (<NSIndexPath: 0xc000000000400116> {length = 2, path = 1 - 2}); frame = (131 178; 50 50); 
cell_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6000001fda00> index path: (<NSIndexPath: 0x600000026760> {length = 2, path = 1 - 2}); frame = (-25.5 178; 363 50); )

supp: [0, 0]
supp_old Attributes: <UICollectionViewLayoutAttributes: 0x6180001fb700> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 0; 375 40); zIndex = 10; 
Section Attributes: <UICollectionViewLayoutAttributes: 0x6000001fe100> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 0; 375 40); zIndex = 10; 
supp_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6000001fe100> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 0; 375 40); zIndex = 10; )

supp: [1, 0]
supp_old Attributes: <UICollectionViewLayoutAttributes: 0x6180001fb800> index path: (<NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 122; 375 40); zIndex = 10; 
Section Attributes: <UICollectionViewLayoutAttributes: 0x6180001fb900> index path: (<NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 122; 375 40); zIndex = 10; 
supp_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6180001fb900> index path: (<NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 122; 375 40); zIndex = 10; )

layoutAttributesForElements: (-667.0, -667.0, 1042.0, 1334.0)

cell: [0, 0]
cell_old Attributes: <UICollectionViewLayoutAttributes: 0x6180001fba00> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); frame = (6 56; 50 50); 
cell_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6180001fc200> index path: (<NSIndexPath: 0x61800002b7a0> {length = 2, path = 0 - 0}); frame = (-150.5 56; 363 50); )

cell: [0, 1]
cell_old Attributes: <UICollectionViewLayoutAttributes: 0x6180001fbb00> index path: (<NSIndexPath: 0xc000000000200016> {length = 2, path = 0 - 1}); frame = (68.5 56; 50 50); 
cell_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6180001fc300> index path: (<NSIndexPath: 0x61800002b820> {length = 2, path = 0 - 1}); frame = (-88 56; 363 50); )

cell: [0, 2]
cell_old Attributes: <UICollectionViewLayoutAttributes: 0x6180001fbc00> index path: (<NSIndexPath: 0xc000000000400016> {length = 2, path = 0 - 2}); frame = (131 56; 50 50); 
cell_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6180001fc400> index path: (<NSIndexPath: 0x61800002b680> {length = 2, path = 0 - 2}); frame = (-25.5 56; 363 50); )

cell: [1, 0]
cell_old Attributes: <UICollectionViewLayoutAttributes: 0x6180001fbd00> index path: (<NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}); frame = (6 178; 50 50); 
cell_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6180001fc500> index path: (<NSIndexPath: 0x61800002b600> {length = 2, path = 1 - 0}); frame = (-150.5 178; 363 50); )

cell: [1, 1]
cell_old Attributes: <UICollectionViewLayoutAttributes: 0x6180001fbe00> index path: (<NSIndexPath: 0xc000000000200116> {length = 2, path = 1 - 1}); frame = (68.5 178; 50 50); 
cell_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6180001fc600> index path: (<NSIndexPath: 0x6180000281a0> {length = 2, path = 1 - 1}); frame = (-88 178; 363 50); )

cell: [1, 2]
cell_old Attributes: <UICollectionViewLayoutAttributes: 0x6180001fbf00> index path: (<NSIndexPath: 0xc000000000400116> {length = 2, path = 1 - 2}); frame = (131 178; 50 50); 
cell_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6180001fc700> index path: (<NSIndexPath: 0x6180000296c0> {length = 2, path = 1 - 2}); frame = (-25.5 178; 363 50); )

supp: [0, 0]
supp_old Attributes: <UICollectionViewLayoutAttributes: 0x6180001fc000> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 0; 375 40); zIndex = 10; 
Section Attributes: <UICollectionViewLayoutAttributes: 0x6180001fc800> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 0; 375 40); zIndex = 10; 
supp_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6180001fc800> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 0; 375 40); zIndex = 10; )

supp: [1, 0]
supp_old Attributes: <UICollectionViewLayoutAttributes: 0x6180001fc100> index path: (<NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 122; 375 40); zIndex = 10; 
Section Attributes: <UICollectionViewLayoutAttributes: 0x6100001fa900> index path: (<NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 122; 375 40); zIndex = 10; 
supp_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6100001fa900> index path: (<NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 122; 375 40); zIndex = 10; )

layoutAttributesForElements: (-667.0, -667.0, 1042.0, 1334.0)

cell: [0, 0]
cell_old Attributes: <UICollectionViewLayoutAttributes: 0x6000003e0e00> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); frame = (6 56; 363 45); 
cell_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6000003e1600> index path: (<NSIndexPath: 0x600000233120> {length = 2, path = 0 - 0}); frame = (6 56; 363 45); )

cell: [0, 1]
cell_old Attributes: <UICollectionViewLayoutAttributes: 0x6000003e0f00> index path: (<NSIndexPath: 0xc000000000200016> {length = 2, path = 0 - 1}); frame = (6 107; 363 45); 
cell_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6000003e1700> index path: (<NSIndexPath: 0x600000233180> {length = 2, path = 0 - 1}); frame = (6 107; 363 45); )

cell: [0, 2]
cell_old Attributes: <UICollectionViewLayoutAttributes: 0x6000003e1000> index path: (<NSIndexPath: 0xc000000000400016> {length = 2, path = 0 - 2}); frame = (6 158; 363 45); 
cell_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6000003e1800> index path: (<NSIndexPath: 0x6000002331a0> {length = 2, path = 0 - 2}); frame = (6 158; 363 45); )

cell: [1, 0]
cell_old Attributes: <UICollectionViewLayoutAttributes: 0x6000003e1100> index path: (<NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}); frame = (6 275; 363 45); 
cell_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6080001fa800> index path: (<NSIndexPath: 0x608000035300> {length = 2, path = 1 - 0}); frame = (6 275; 363 45); )

cell: [1, 1]
cell_old Attributes: <UICollectionViewLayoutAttributes: 0x6000003e1200> index path: (<NSIndexPath: 0xc000000000200116> {length = 2, path = 1 - 1}); frame = (6 326; 363 45); 
cell_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6080001fb200> index path: (<NSIndexPath: 0x608000035500> {length = 2, path = 1 - 1}); frame = (6 326; 363 45); )

cell: [1, 2]
cell_old Attributes: <UICollectionViewLayoutAttributes: 0x6000003e1300> index path: (<NSIndexPath: 0xc000000000400116> {length = 2, path = 1 - 2}); frame = (6 377; 363 45); 
cell_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6080001fb100> index path: (<NSIndexPath: 0x608000034d00> {length = 2, path = 1 - 2}); frame = (6 377; 363 45); )

supp: [0, 0]
supp_old Attributes: <UICollectionViewLayoutAttributes: 0x6000003e1400> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 0; 375 40); zIndex = 10; 
Section Attributes: <UICollectionViewLayoutAttributes: 0x6000003e1900> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 0; 375 40); zIndex = 10; 
supp_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6000003e1900> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 0; 375 40); zIndex = 10; )

supp: [1, 0]
supp_old Attributes: <UICollectionViewLayoutAttributes: 0x6000003e1500> index path: (<NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 219; 375 40); zIndex = 10; 
Section Attributes: <UICollectionViewLayoutAttributes: 0x6000003e1a00> index path: (<NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 219; 375 40); zIndex = 10; 
supp_new Attributes: Optional(<UICollectionViewLayoutAttributes: 0x6000003e1a00> index path: (<NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 219; 375 40); zIndex = 10; )