【发布时间】:2014-11-11 06:03:23
【问题描述】:
似乎单元格在内部加载,但 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(@"%@",[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>>
【问题讨论】:
-
在IB中改变Cell的ContentView大小时可以显示。
标签: ios objective-c xcode uicollectionview uicollectionviewcell