Ios 使用CollectionView隐藏单元格

Ios 使用CollectionView隐藏单元格,ios,objective-c,uicollectionview,collectionview,Ios,Objective C,Uicollectionview,Collectionview,我有一个导航控制器推送到集合视图控制器。这个collectionView有一个图像的NSArray,我确信collectionView中有一些元素使用了visibleCells方法。 但当我启动它时,没有显示单元格。 这是我的密码: - (void)viewDidLoad { [super viewDidLoad]; images = [NSArray arrayWithObjects:@"angry_birds_cake.jpg", @"creme_brelee.jpg", @"egg

我有一个导航控制器推送到集合视图控制器。这个collectionView有一个图像的
NSArray
,我确信collectionView中有一些元素使用了visibleCells方法。 但当我启动它时,没有显示单元格。 这是我的密码:

- (void)viewDidLoad
{
    [super viewDidLoad];
images = [NSArray arrayWithObjects:@"angry_birds_cake.jpg", @"creme_brelee.jpg", @"egg_benedict.jpg", @"full_breakfast.jpg", @"green_tea.jpg", @"ham_and_cheese_panini.jpg", @"ham_and_egg_sandwich.jpg", @"hamburger.jpg", @"instant_noodle_with_egg.jpg", @"japanese_noodle_with_pork.jpg", @"mushroom_risotto.jpg", @"noodle_with_bbq_pork.jpg", @"starbucks_coffee.jpg", @"thai_shrimp_cake.jpg", @"vegetable_curry.jpg", @"white_chocolate_donut.jpg", nil];
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";

UICollectionViewCell *cell;
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];


NSArray *tab = [collectionView visibleCells];
for (int i = 0; i < [tab count]; ++i)
{
    NSLog(@"%d", i);
}
[collectionView setBackgroundColor:[UIColor blueColor]];

UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
imageView.image = [UIImage imageNamed:[images objectAtIndex:indexPath.row]];


return cell;
}
-(void)viewDidLoad
{
[超级视图下载];
图片=[NSArray arrayWithObjects:@“愤怒的小鸟”蛋糕.jpg“,@“creme_brelee.jpg“,@“egg_benedict.jpg“,@“full_早餐.jpg“,@“green_tea.jpg“,@“ham_and_cheese_panini.jpg”,@“ham_and_egg三明治.jpg”,@“hamburger.jpg”,@“含蛋的方便面.jpg”,@“含猪肉的日本_面条.jpg”@"星巴克咖啡(starbucks_coffee.jpg),"泰式虾仁(thai_shrimp)蛋糕(jpg),"蔬菜(plant)咖喱(jpg),34;
} 
-(NSInteger)collectionView:(UICollectionView*)collectionView项目编号截面:(NSInteger)截面{
返回图像。计数;
}
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*标识符=@“单元格”;
UICollectionViewCell*单元格;
[collectionView注册表类:[UICollectionViewCell类]forCellWithReuseIdentifier:@“Cell”];
cell=[collectionView-dequeueReusableCellWithReuseIdentifier:indexPath的标识符:indexPath];
NSArray*选项卡=[collectionView visibleCells];
对于(int i=0;i<[制表符计数];++i)
{
NSLog(@“%d”,i);
}
[collectionView setBackgroundColor:[UIColor blueColor]];
UIImageView*imageView=(UIImageView*)[带标记的单元格视图:100];
imageView.image=[UIImage ImageName:[images objectAtIndex:indexPath.row]];
返回单元;
}

我哪里错了?非常感谢。

在尝试设置图像之前,请检查您是否确实有
imageView
。插入
NSAssert(imageView,@“必须有一个imageView”);
介于
UIImageView*imageView=…
imageView.image=…
[collectionView注册表类:[UICollectionViewCell类]forCellWithReuseIdentifier:@“单元格”];
应该在viewDidLoad或类似程序中完成,不应该每次加载单元格时调用它,应该只调用一次。感谢您的回复。NSAssert引发了一个异常,我如何修复它以获得图像?我不知道如何只调用一次此方法。我必须在collectionView重写时调用该方法,不是吗?好的,我添加了一个在viewDidLoad方法中使用register类编辑该方法,但它也不起作用。您必须创建一个自定义UICollectionViewCell子类,其中包含UIImageView。