collectionview自定义单元格没有出现

时间:2015-10-01 09:43:57

标签: ios objective-c uicollectionviewcell collectionview

我使用自定义单元格创建collectionview并且工作正常,但是当我在应用程序中更新它无法正常工作时,我不知道原因。

[self.collectionView registerClass:[CellItem class] forCellWithReuseIdentifier:@"CellItem"];
[self.collectionView setDataSource:self];
[self.collectionView setDelegate:self];

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CellItem* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellItem"
                                                           forIndexPath:indexPath];
   //cell.backgroundColor = [UIColor redColor];
   NSDictionary* celldata = [productsDictionary objectAtIndex:indexPath.row];
   NSString* productName = [celldata objectForKey:@"name"];
   NSString* productImage = [celldata objectForKey:@"image"];

   dispatch_async(dispatch_get_global_queue(0,0), ^{

    NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: productImage]];
      if (data == nil)
        return;
      dispatch_async(dispatch_get_main_queue(), ^{

        // WARNING: is the cell still using the same data by this point??

       cell.imageViewProduct.image = [UIImage imageWithData: data];

        //cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData: data]];
      });
  });
  [cell.lableTitle setText:productName];
  return cell;
}

和itemcell类:

@property (weak, nonatomic) IBOutlet UITextView *lableTitle;

@property (weak, nonatomic) IBOutlet UIImageView *imageViewProduct;

此处的输出图片: http://i.stack.imgur.com/bcZaw.jpg

3 个答案:

答案 0 :(得分:1)

请尝试删除

[self.collectionView registerClass:[CellItem class] forCellWithReuseIdentifier:@"CellItem"];

您使用自定义UICollectionViewCell,因此不应使用registerClass

答案 1 :(得分:0)

在故事板中尝试将一个原型单元格添加到集合视图中。然后在身份检查器中指定您的类和属性检查器单元标识符。

UITableView示例(与UICollectionView相同): enter image description here enter image description here

答案 2 :(得分:0)

尝试使用寄存器nib函数而不是寄存器类。

[collectionView registerNib:[UINib nibWithNibName:@"你的手机笔尖名称" bundle:nil] forCellWithReuseIdentifier:@"你的小区标识符"];

让我知道它是否适合你。