UICollectionViewCell不显示单元格

时间:2013-11-22 00:30:25

标签: ios uicollectionview

我创建了一个自定义UICollectionViewCell,你可以看到每个单元格的创建没有任何问题,可以找到xib,但屏幕上没有显示任何内容。

我已连接了ProgrammaCell的xib中委托的数据源。这是我的代码......

我的CollectionView被称为“griglia”,并在xib中连接

@interface GrigliaProgrammiViewController : UIViewController <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{    XMLParser *xmlParser;
}

....

@end

- (void)viewDidLoad
{
[super viewDidLoad];
   .....
[self.griglia registerClass:[ProgrammaCell class] forCellWithReuseIdentifier:@"prg"];
self.griglia.dataSource = self;
self.griglia.delegate = self;
xmlParser = [[XMLParser alloc]
loadXMLByURL:@"..."];
[self.griglia reloadInputViews];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:    (NSInteger)section {
return [[xmlParser programmi] count];}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{


ProgrammaCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"prg" forIndexPath:indexPath];
if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Programma" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

Programmi *programma = [[xmlParser programmi] objectAtIndex:indexPath.row];

cell.titolo.text= programma.titolo;
cell.giorno.text = programma.programma_giorno;
cell.orario.text = programma.programma_orario;


UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = cell.immagine.bounds;
[cell.immagine addSubview:activityIndicator];
[cell.immagine setImageWithURL:[NSURL URLWithString:programma.programma_copertina]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                      completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                      [activityIndicator removeFromSuperview];
                      }];
return cell;
}

3 个答案:

答案 0 :(得分:0)

您是如何初始化UICollectionView的?你使用什么类型的布局?我发现如果布局的itemsize太大,有时候不会调用cellForItemAtIndexPath。

请参阅问题UICollectionView's cellForItemAtIndexPath is not being called

答案 1 :(得分:0)

您是否可以尝试在(void)viewDidLoad

中使用此代码段
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Programma" owner:self options:nil];
[self.griglia registerNib:nib forCellWithReuseIdentifier:@"prg"];

而不是原始代码:

[self.griglia registerClass:[ProgrammaCell class] forCellWithReuseIdentifier:@"prg"];

ProgrammaCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"prg" forIndexPath:indexPath];
//if (cell == nil) {
//    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Programma" owner:self options:nil];
//    cell = [nib objectAtIndex:0];
//}

我的猜测是registerClass:forCellWithReuseIdentifier:方法将您的类注册为可重用单元格,但您的单元类仅描述了单元格应该执行的操作(单元格的行为)。只有Nib文件描述了应该如何呈现单元格(单元格的UI)。

只是我的猜测,试一试。 :)

答案 2 :(得分:0)

来自笔尖的加载单元应该这样做:

[tableView registerNib:[UINib nibWithNibName:@"YourCellNibName" bundle:nil] forCellReuseIdentifier:JXVideoLikeMsgCellID];

不应从NSBundle加载

[[NSBundle mainBundle] loadNibNamed:@"YourCellNibName" owner:nil options:nil];

UICollectionView相同。