似乎单元格在内部加载但是,UICollectionView不会出现。
CollectionViewCell.xib and CollectionResuableView.xib
也包含在项目中。
CollectionView是黑色的遗骸。
为什么它不起作用?我使用Xcode 6并使用下面的代码。
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
#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(@&#34;%@&#34;,[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>>
答案 0 :(得分:0)
Collectionview没有出现所以我认为在collectionview布局中存在问题所以首先在viewcontroller中实现委托UICollectionViewDelegateFlowLayout并尝试添加一些委托方法来检查它是否正确地触发它的委托以便它可以帮助你。
答案 1 :(得分:0)
在两个工作日中,当所有委托方法等工作时,我花了UICollectionView搜索“黑色矩形”的原因,我两次读取每个SO条目关于“UICollectionView”和“黑色”并且有许多仍未得到答复。选择此问题作为更新的问题:在IB / XIB,故事板,UICollectionView的属性检查器中,向下滚动到视图部分,将背景从默认更改为另一种颜色,然后重建。
我得出结论,经常发生在第一次测试UICollectionView以查看是否可以使用它时,使用默认的Label(黑色文本)和可能的默认Image(可能还没有为其分配实际图像,或者可能是暗色或透明) )在UICollectionViewCell中,模拟器显示黑色矩形,因此当问题是简单图形时,您可以断定新代码被破坏。