【问题标题】:UICollectionview is emptyUICollectionview 为空
【发布时间】:2013-11-07 06:47:25
【问题描述】:

您好,我创建了一个UIViewController,其中我添加了一个带有CustomCell 的UICollectionView。然后,我为单元格创建了一个uicollectionviewcell 类。最后在 storybord 本身中设置委托和数据源。但它显示空白页面。如下图所示

代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.collectionview = _collectionview;
    self.collectionview.dataSource = self;
    self.collectionview.delegate = self;

    speakersImages=[[NSMutableArray alloc]init];
    // Do any additional setup after loading the view, typically from a nib.
    imageLabels=[[NSMutableArray alloc]initWithObjects:@"RB Forum 2013",@"Agenda",@"Speakers",@"Contact",@"Map",@"Websites",@"@Responsible Biz",@"Partners",@"Sustainability",nil];


    imagePaths=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"RBform.png"],[UIImage imageNamed:@"agenda.png"],[UIImage imageNamed:@"speakers.png"],[UIImage imageNamed:@"contact.png"],[UIImage imageNamed:@"map.png"],[UIImage imageNamed:@"website.png"],[UIImage imageNamed:@"twitter.png"],[UIImage imageNamed:@"partners.png"],[UIImage imageNamed:@"sustainability.png"], nil];
    [self.collectionview registerClass:[NewCell class] forCellWithReuseIdentifier:@"Cell"];



}

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

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

//THE BELOW DELEGATE METHOD DOES NOT GET CALLED!


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // we're going to use a custom UICollectionViewCell, which will hold an image and its label
    //
    static NSString *CellIdentifier = @"Cell";
    NewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    // make the cell's title the actual NSIndexPath value

    cell.labels.text = [NSString stringWithFormat:@"%@",[imageLabels objectAtIndex:indexPath.row]];
    // load the image for this cell
    NSLog(@"%@",imageLabels);
    //NSString *imageToLoad = [NSString stringWithFormat:@"%d.JPG", indexPath.row];
    //    cell.image.image = [UIImage imageNamed:imageToLoad];
    cell.images.image = [imagePaths objectAtIndex:indexPath.row];

    return cell;

}

【问题讨论】:

  • 你的 NewCell 是自定义类吗?
  • YES Deepak 它是我的客户类
  • 是否调用了任何数据源方法?
  • 是什么意思?我没找到你?

标签: ios storyboard nsarray uicollectionview uicollectionviewcell


【解决方案1】:

解决方案是删除 [self.collectionview registerClass:[NewCell 类] forCellWithReuseIdentifier:@"Cell"];

在storybord中我们不需要这个

【讨论】:

  • 你确定吗?在我使用自定义 collectionview 单元格(Class + XIB)的情况下,我发现我必须同时注册 Class 和 Nib。注册 NIB 允许视图显示,但注册类强制 [dequeue] 返回自定义单元格。也许是因为我的自定义类没有加载 NIB,但 XIB 将集合视图单元格的类设置为我的自定义类。
【解决方案2】:

代码没问题,但顺序不同。在填充数组之前分配数据源和委托会使单元格的数量返回为 0,因为尚未填充数组

这样试试

    speakersImages=[[NSMutableArray alloc]init];
    // Do any additional setup after loading the view, typically from a nib.
    imageLabels=[[NSMutableArray alloc]initWithObjects:@"RB Forum 2013",@"Agenda",@"Speakers",@"Contact",@"Map",@"Websites",@"@Responsible Biz",@"Partners",@"Sustainability",nil];


    imagePaths=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"RBform.png"],[UIImage imageNamed:@"agenda.png"],[UIImage imageNamed:@"speakers.png"],[UIImage imageNamed:@"contact.png"],[UIImage imageNamed:@"map.png"],[UIImage imageNamed:@"website.png"],[UIImage imageNamed:@"twitter.png"],[UIImage imageNamed:@"partners.png"],[UIImage imageNamed:@"sustainability.png"], nil];
    [self.collectionview registerClass:[NewCell class] forCellWithReuseIdentifier:@"Cell"];
   [self.collectionview reloadData];

这行不需要

self.collectionview = _collectionview;
self.collectionview.dataSource = self;
    self.collectionview.delegate = self;

还要确保您的收藏视图通过插座连接

确保<UICollectioniewDatasource,UICollectionviewDelegate> 在类头中

【讨论】:

  • 出口是否正确,数据源和委托是否通过 ib 连接?
  • 去掉自定义类名,改成uicollectionviewcell
  • 应用程序崩溃并显示以下消息 [UICollectionViewCell 标签]:无法识别的选择器发送到实例 0x8d5ba50 2013-11-07 12:51:15.528 RBForm[4590:a0b] *** 由于未捕获的异常而终止应用程序' NSInvalidArgumentException',原因:'-[UICollectionViewCell 标签]:无法识别的选择器发送到实例 0x8d5ba50'
【解决方案3】:
  1. 在你的故事板中给你的imageview 一个tag 例如1,以及您的label 另一个tag,例如2

  2. 提供您已经提供的 cell 重用标识符,例如。 "Cell"

  3. 现在在您的故事板中右键单击collection view 并为您的视图控制器或文件所有者创建datasource 和delegate。或者,您可以在 viewController 中使用 <UICollectionViewDataSource,UICollectionViewDelegate> 并将委托和数据源设置为 self。

  4. 现在在你的 viewDidLoad 中

-(void)viewDidLoad{

            self.collectionview.dataSource = self;
            self.collectionview.delegate = self;
        
            speakersImages=[[NSMutableArray alloc]init];
            // Do any additional setup after loading the view, typically from a nib.
            imageLabels=[[NSMutableArray alloc]initWithObjects:@"RB Forum   2013",@"Agenda",@"Speakers",@"Contact",@"Map",@"Websites",@"@Responsible     Biz",@"Partners",@"Sustainability",nil];
        
        
            imagePaths=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"RBform.png"],[UIImage imageNamed:@"agenda.png"],[UIImage imageNamed:@"speakers.png"],[UIImage imageNamed:@"contact.png"],[UIImage imageNamed:@"map.png"],[UIImage imageNamed:@"website.png"],[UIImage imageNamed:@"twitter.png"],[UIImage imageNamed:@"partners.png"],[UIImage imageNamed:@"sustainability.png"], nil];
        
        }
  1. 集合视图中的项目数

    • (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)部分 {

            return [imageLabels count];
        }
      
  2. 创建单元并重用

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

                    UICollectionView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell"];
      
                    // make the cell's title the actual NSIndexPath value
      
                    UIlabel *label = (UIlabel *) [cell viewWithTag:2]
                    label.text = [NSString stringWithFormat:@"%@",[imageLabels   objectAtIndex:indexPath.row]];
      
                    // load the image for this cell
                    UIImageView *imageView = (UIImageView *) [cell viewWithTag:1]
                    imageView.image = [imagePaths objectAtIndex:indexPath.row];
      
                    return cell;
      
                }
      

【讨论】:

    【解决方案4】:
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
     [self.collectionView registerNib:[UINib nibWithNibName:@"NewCell" bundle:nil] forCellWithReuseIdentifier:@"CELL"];
    
        self.collectionview.dataSource = self;
        self.collectionview.delegate = self;
    
    
    self.imageLabels = [NSArray arrayWithObjects:@"RB Forum 2013",@"Agenda",@"Speakers",@"Contact",@"Map",@"Websites",@"@Responsible Biz",@"Partners",@"Sustainability",nil];
    
    self.imagePaths=[NSArray arrayWithObjects::@"RBform.png",@"agenda.png",@"speakers.png",@"contact.png",@"map.png",@"website.png",@"twitter.png",@"partners.png",@"sustainability.png", nil];
    
    
    
    }
    
    - (NSInteger)collectionView:(UICollectionView *)collectionView
         numberOfItemsInSection:(NSInteger)section
    {
        return [self.imageLabels count];
    }
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    
        NewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
    
        cell.labels.text = [self.imageLabels objectAtIndex:indexPath.row]];
        cell.images.image = [UIImage imageNamed:[self.imagePaths objectAtIndex:index path.row]];
    
        return cell;
    
    }
    

    【讨论】:

    • 由于未捕获的异常“NSInternalInconsistencyException”而崩溃并显示以下消息,原因:“无法在捆绑包中加载 NIB:'NSBundle /NAVEEN/Library/Application Support/iPhone Simulator/7.0/Applications /E1951DA5-05BA-4D6C-B560-F160BC373249/RBForm.app>(已加载)',名称为'NewCell''
    • 给你的 UICollectionViewCell 名字
    • 你是为 NewCell 创建的 xib 吗?
    • 我没有使用 Storybord...你能给你的邮件 id 然后我可以把这个文件发给你
    猜你喜欢
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多