UICollectionview自我调整单元格不起作用

时间:2015-03-08 09:15:27

标签: ios objective-c uicollectionview uicollectionviewlayout

我想在我的集合视图中使用自定义单元格。 我的细胞不使用自动布局 -

我已根据需要实施了这些方法,但仍未正确调整大小。

附件是代码:

    @interface Cell : UICollectionViewCell

    @end

    @implementation Cell

    -(CGSize)sizeThatFits:(CGSize)size {
        return CGSizeMake(320, 200);
    }

// I have tried with and without this
-(UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    UICollectionViewLayoutAttributes *attributes = [super preferredLayoutAttributesFittingAttributes:layoutAttributes];
    CGRect frame = attributes.frame;
    frame.size = CGSizeMake(320, 200);
    return attributes;
}


    @end

    @interface ViewController () <UICollectionViewDataSource>

    @property (nonatomic,strong) UICollectionView *collectionView;

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
        [self.view addSubview:self.collectionView];
        self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"cell"];
        self.collectionView.dataSource = self;
        layout.estimatedItemSize = CGSizeMake(320, 10);
        // Do any additional setup after loading the view, typically from a nib.
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    #pragma mark -
    #pragma mark UICollectionViewDataSource

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
        return 10;
    }

    // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor redColor];
        return cell;

    }

同样在旋转设备时,我遇到了崩溃:

*** Assertion failure in -[_UIFlowLayoutSection computeLayoutInRect:forSection:invalidating:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UIFlowLayoutSupport.m:763
2015-03-08 11:09:06.430 TestSelfSizingCells[77344:3177236] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionViewFlowLayout internal error'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010881bf35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001084b4bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010881bd9a +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x00000001080d15df -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
    4   UIKit                               0x0000000109298e53 -[_UIFlowLayoutSection computeLayoutInRect:forSection:invalidating:] + 9052
    5   UIKit                               0x000000010925f96c __56-[UICollectionViewFlowLayout _updateItemsLayoutForRect:]_block_invoke + 509
    6   CoreFoundation                      0x0000000108754042 __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 114
    7   CoreFoundation                      0x000000010875379c -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 300
    8   UIKit                               0x000000010925f69c -[UICollectionViewFlowLayout _updateItemsLayoutForRect:] + 780
    9   UIKit                               0x000000010925fce2 -[UICollectionViewFlowLayout _fetchItemsInfoForRect:] + 583
    10  UIKit                               0x000000010925cde6 -[UICollectionViewFlowLayout _layoutAttributesForItemsInRect:] + 646
    11  UIKit                               0x000000010925e268 -[UICollectionViewFlowLayout layoutAttributesForElementsInRect:] + 119
    12  UIKit                               0x0000000109274130 __45-[UICollectionViewData validateLayoutInRect:]_block_invoke + 144
    13  UIKit                               0x0000000109273627 -[UICollectionViewData validateLayoutInRect:] + 1396
    14  UIKit                               0x000000010923b7b8 -[UICollectionView layoutSubviews] + 170
    15  UIKit                               0x0000000108c81973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
    16  QuartzCore                          0x000000010c4f6de8 -[CALayer layoutSublayers] + 150
    17  QuartzCore                          0x000000010c4eba0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    18  QuartzCore                          0x000000010c4f6d1d -[CALayer layoutIfNeeded] + 162
    19  UIKit                               0x0000000108d4374a -[UIViewController window:willAnimateRotationToInterfaceOrientation:duration:newSize:] + 211
    20  UIKit                               0x0000000108d47e84 __95-[UIViewController(AdaptiveSizing) _window:viewWillTransitionToSize:withTransitionCoordinator:]_block_invoke2889 + 73
    21  UIKit                               0x0000000109361a0e -[_UIViewControllerTransitionCoordinator _applyBlocks:releaseBlocks:] + 217
    22  UIKit                               0x000000010935ef2e -[_UIViewControllerTransitionContext __runAlongsideAnimations] + 139
    23  UIKit                               0x00000001090964d4 __58-[_UIWindowRotationAnimationController animateTransition:]_block_invoke + 96
    24  UIKit                               0x0000000108c7a362 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 473
    25  UIKit                               0x0000000108c7a5b7 +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:] + 57
    26  UIKit                               0x0000000109096430 -[_UIWindowRotationAnimationController animateTransition:] + 408
    27  UIKit                               0x0000000108c52704 -[UIWindow _rotateToBounds:withAnimator:transitionContext:] + 761
    28  UIKit                               0x0000000108c54ac0 -[UIWindow _rotateWindowToOrientation:updateStatusBar:duration:skipCallbacks:] + 2134
    29  UIKit                               0x0000000108c55092 -[UIWindow _setRotatableClient:toOrientation:applyTransformToWindow:updateStatusBar:duration:force:isRotating:] + 559
    30  UIKit                               0x0000000108c541ee -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 116
    31  UIKit                               0x0000000108c54174 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 36
    32  UIKit                               0x0000000108c54021 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 122
    33  UIKit                               0x0000000108c53100 __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 98
    34  UIKit                               0x0000000108c53079 -[UIWindow _updateToInterfaceOrientation:duration:force:] + 386
    35  UIKit                               0x0000000108c53450 -[UIWindow _updateInterfaceOrientationFromDeviceOrientation:] + 316
    36  CoreFoundation                      0x00000001087ebcec __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
    37  CoreFoundation                      0x00000001086eb8a4 _CFXNotificationPost + 2484
    38  Foundation                          0x00000001080126b8 -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
    39  UIKit                               0x0000000108e70efc -[UIDevice setOrientation:animated:] + 282
    40  UIKit                               0x0000000108c1c476 -[UIApplication handleEvent:withNewEvent:] + 1474
    41  UIKit                               0x0000000108c1c90a -[UIApplication sendEvent:] + 79
    42  UIKit                               0x0000000108c086af _UIApplicationHandleEvent + 593
    43  GraphicsServices                    0x000000010bdec33e _PurpleEventCallback + 756
    44  GraphicsServices                    0x000000010bdebe26 PurpleEventCallback + 35
    45  CoreFoundation                      0x00000001087517c9 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
    46  CoreFoundation                      0x000000010875173b __CFRunLoopDoSource1 + 475
    47  CoreFoundation                      0x0000000108746f88 __CFRunLoopRun + 2200
    48  CoreFoundation                      0x0000000108746486 CFRunLoopRunSpecific + 470
    49  GraphicsServices                    0x000000010bdea9f0 GSEventRunModal + 161
    50  UIKit                               0x0000000108c08420 UIApplicationMain + 1282
    51  TestSelfSizingCells                 0x0000000107f87853 main + 115
    52  libdyld.dylib                       0x000000010adab145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

我还注意到,当使用不同的屏幕尺寸时,效果非常好。例如在iPhone 5中,单元格看起来好了#34;在一开始的时候。但过了一会儿他们搞砸了。

同样在iPhone 6模拟器中,集合视图根本不会滚动,直到我来回旋转设备,然后最终应用程序崩溃。

enter image description here

0 个答案:

没有答案