似乎单元格是在内部加载的,但是,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;
@endViewController.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;
}
@endNSLog(%@,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>>发布于 2014-11-11 11:23:40
集合视图没有出现,所以我认为在集合视图布局中存在问题,所以首先在视图控制器中实现委托UICollectionViewDelegateFlowLayout,并尝试添加很少的集合视图委托方法,以检查它是否正确地触发了其委托,这样它将对您有所帮助。
发布于 2015-02-11 02:09:26
在我搜索UICollectionView的“黑色矩形”原因的两个工作日里,当所有委托方法等都在工作时,我每读一次关于"UICollectionView“和”黑色“的条目,还有很多还没有回答。选择这个问题是最近的:在IB/XIB、Storyboard中,UICollectionView的属性检查器,向下滚动到View部分,将背景从默认更改为另一种颜色,然后重新构建。
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.https://stackoverflow.com/questions/26858582
复制相似问题