我正在尝试使用UICollectionView,但我遇到了很多问题。我有一个UIViewController里面有一个UICollectionView。我已经正确设置了所有代表以及CVcell中的UILabel。视图显示并且在代码中定义为白色。 NSLog发出所有单元格和应该存在的文本,但在实际的UICollectionView中它是空白的,并且除了滚动视图之外没有任何内容。我已经尝试了一切,但似乎无法正常工作。单元格为187 x 187,占据屏幕尺寸的一半,标签位于中间。我将在下面发布代码。
由于
#import "StudentControllerMain.h"
#import "CVcell.h"
@interface StudentControllerMain ()
@property (nonatomic, strong) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *dataArray;
@end
@implementation StudentControllerMain
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *firstSection = [[NSMutableArray alloc] init];
for (int i=0; i<5; i++) {
[firstSection addObject:[NSString stringWithFormat:@"Cell %d", i]];
}
self.dataArray = [[NSArray alloc] initWithObjects:firstSection, nil];
[self.collectionView registerClass:[CVcell class] forCellWithReuseIdentifier:@"CVcell"];
// Configure layout
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
self.collectionView = nil;
self.dataArray = nil;
// Dispose of any resources that can be recreated.
}
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return [self.dataArray count];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
NSMutableArray *sectionArray = [self.dataArray objectAtIndex:section];
return [sectionArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
// Setup cell identifier
static NSString *cellIdentifier = @"CVcell";
CVcell *cell = (CVcell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section];
NSString *cellData = [data objectAtIndex:indexPath.row];
NSLog(cellData);
[cell.title setText:cellData];
[cell.title setTextColor:[UIColor blackColor]];
return cell;
}
@end
答案 0 :(得分:1)
答案 1 :(得分:0)
如果你确定正在调用cellForItemAtIndexPath,那么问题就是所有视图的框架。运行该应用程序并转到该页面。转到调试 - &gt;查看调试 - &gt;捕获视图层次结构这将显示所有视图及其大小。你看到桌子了吗?它是正确的高度吗?你能看到每个细胞吗?你能看到他们的子视图吗?您可以右键单击每个以打印其尺寸。