【问题标题】:UICollectionViewCell don't display cellUICollectionViewCell 不显示单元格
【发布时间】:2013-12-06 17:37:50
【问题描述】:

如您所见,我创建了一个自定义 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;
}

【问题讨论】:

    标签: ios uicollectionview


    【解决方案1】:

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

    见问题UICollectionView's cellForItemAtIndexPath is not being called

    【讨论】:

      【解决方案2】:

      你能尝试在 (void)viewDidLoad 中使用这段代码 sn-p

      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)。

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

      【讨论】:

      • 感谢您的评论.. 我试了一下.. 由于未捕获的异常 'NSInvalidArgumentException' 导致其他错误终止应用程序,原因:'-[ProgrammaCell instantiateWithOwner:options:]: unrecognized selector sent to instance 0xe13a380 '
      • 错误在 ProgrammaCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"prg" forIndexPath:indexPath];
      • 它应该是[tableView registerNib:[UINib nibWithNibName:@"JXVideoLikeMsgCell" bundle:nil] forCellReuseIdentifier:JXVideoLikeMsgCellID];,而不是从 NSBundle 加载。
      【解决方案3】:

      来自笔尖的称重传感器应该这样做:

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

      不应从 NSBundle 加载:

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


      UICollectionView相同。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-18
        • 1970-01-01
        • 2016-03-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多