简体   繁体   English

UICollectionView单元内部加载,但未显示UICollectionView

[英]UICollectionView Cell Internally loaded but UICollectionView is not Showing

Seems cell is loaded internally but , UICollectionView does not appear . 似乎单元是在内部加载的,但是,不会显示UICollectionView。

CollectionViewCell.xib and CollectionResuableView.xib also be included in the project . CollectionViewCell.xib and CollectionResuableView.xib也包含在项目中。

CollectionView is the remains of black. CollectionView是黑色的残骸。

Why it is not working? 为什么不起作用? I have use Xcode 6 and use below code. 我使用Xcode 6,并使用以下代码。


ViewController.h ViewController.h

#import <UIKit/UIKit.h>
#import "CollectionReusableView.h"
#import "CollectionViewCell.h"
@interface ViewController : UIViewController<UICollectionViewDelegate,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

ViewController.m ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _setup];
}

- (void)_setup{
    self.collectionView.delegate = (id)self;
    self.collectionView.dataSource = (id)self;
     [self.collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"Cell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"CollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Section"];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        CollectionReusableView *sectionView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Section" forIndexPath:indexPath];
        sectionView.label.text = [NSString stringWithFormat:@"Section%ld", indexPath.section + 1];
        return sectionView;
    } else {
        return nil;
    }
}

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

- (CollectionViewCell *) collectionView:(UICollectionView *)collectionView
                          cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"Cell" forIndexPath:indexPath];
    cell.label.text = [NSString stringWithFormat:@"Row: %li",(long)indexPath.row];
    NSLog(@"%@",[cell.label debugDescription]);
    return cell;
}
@end

NSLog(@"%@",[cell.label debugDescription]); NSLog(@“%@”,[cell.label debugDescription]);

2014-11-11 14:54:46.899 collectionViewSample[7242:1226917] <UILabel: 0x7fd492c54af0; frame = (58 14; 42 21); text = 'Row: 0'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fd492c54c50>>
2014-11-11 14:54:46.900 collectionViewSample[7242:1226917] <UILabel: 0x7fd492c58850; frame = (58 14; 42 21); text = 'Row: 1'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fd492c589b0>>
2014-11-11 14:54:46.900 collectionViewSample[7242:1226917] <UILabel: 0x7fd492c5c5c0; frame = (58 14; 42 21); text = 'Row: 2'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fd492c5c720>>
2014-11-11 14:54:46.901 collectionViewSample[7242:1226917] <UILabel: 0x7fd492c60360; frame = (58 14; 42 21); text = 'Row: 3'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fd492c604c0>>
2014-11-11 14:54:46.901 collectionViewSample[7242:1226917] <UILabel: 0x7fd492c64130; frame = (58 14; 42 21); text = 'Row: 4'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fd492c64290>>
2014-11-11 14:54:46.902 collectionViewSample[7242:1226917] <UILabel: 0x7fd492c67d20; frame = (58 14; 42 21); text = 'Row: 5'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fd492c67e80>>
2014-11-11 14:54:46.902 collectionViewSample[7242:1226917] <UILabel: 0x7fd492c6bac0; frame = (58 14; 42 21); text = 'Row: 6'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fd492c6bc20>>
2014-11-11 14:54:46.903 collectionViewSample[7242:1226917] <UILabel: 0x7fd492c6f8a0; frame = (58 14; 42 21); text = 'Row: 7'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fd492c6fa00>>
2014-11-11 14:54:46.903 collectionViewSample[7242:1226917] <UILabel: 0x7fd492c73610; frame = (58 14; 42 21); text = 'Row: 8'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fd492c73770>>
2014-11-11 14:54:46.904 collectionViewSample[7242:1226917] <UILabel: 0x7fd492c771d0; frame = (58 14; 42 21); text = 'Row: 9'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fd492c77330>>

Collectionview没有出现,所以我认为collectionview布局中存在问题,因此首先在您的viewcontroller中实现委托UICollectionViewDelegateFlowLayout并尝试添加一些collectionview的委托方法来检查它是否正确触发了它的委托,因此它将为您提供帮助。

In the two work days I spent searching for the cause of "black rectangle" for UICollectionView when all delegate methods etc. are working, I twice read every SO entry about "UICollectionView" and "black" and there are many still unanswered. 在两个工作日中,当所有委托方法等都在工作时,我花了时间寻找UICollectionView的“黑色矩形”的原因,我两次阅读了有关“ UICollectionView”和“ black”的每个SO条目,但仍有许多问题没有答案。 Choosing this question as more recent: in IB/XIB, Storyboard, Attributes Inspector for the UICollectionView, scroll down to the View section, change Background from Default to another color, and rebuild. 选择这个问题是最近的:在IB / XIB,情节提要,UICollectionView的属性检查器中,向下滚动到“视图”部分,将“背景”从“默认”更改为另一种颜色,然后重新构建。
I conclude it frequently happens that when first testing a UICollectionView to see if you can make it work, with a default Label (black text) and possibly default Image (possibly without yet assigning an actual image to it, or possibly of dark color or transparent) in the UICollectionViewCell, the simulator displays the black rectangle, so you conclude your new code is broken when problem is simple graphics. 我得出的结论是,经常发生这样的情况:在第一次测试UICollectionView时,是否可以使用默认的Label(黑色文本)和可能的默认Image(可能尚未为其分配实际图像,或者是深色或透明) )在UICollectionViewCell中,模拟器显示黑色矩形,因此当问题是简单图形时,您可以得出结论,新代码已损坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM