未调用 UICollectionView cellForItemAtIndexPath 方法

UICollectionView cellForItemAtIndexPath method is not getting called

cellForItemAtIndexPath 未被调用。我觉得一切都很正常 有什么问题?

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

-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell=(UICollectionView *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    cell.titleText.text=@"test";

    return cell;
}

在您的 viewDidLoad 方法中,您可能想要添加:

self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView reloadData];

就这么简单。

创建collectionView有很多亮点,比如UICollectionViewFlowLayout,然后实现没有0return值的dataSource函数

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

如果您不向集合视图提供内容大小信息,则不会调用 cellForItemAtIndexPath:。覆盖 collectionViewContentSize 并在那里提供尺寸,它应该可以解决您的问题。

完全给你打码,跟着that.You就能解决

在.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
{
   NSMutableArray *imgArray;
   NSMutableArray *lblArray;
}


@property (strong, nonatomic) IBOutlet UICollectionView *collections;

@end

.m

//If you prefer the custom cell for collection view,just import that

#import "customCell.h"

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize collections;

- (void)viewDidLoad
{
    [super viewDidLoad];

    imgArray = [NSMutableArray arrayWithObjects:
            [UIImage imageNamed:@"iPhone.jpg"],
            [UIImage imageNamed:@"iPad.jpg"],
            [UIImage imageNamed:@"iPod.jpg"],
            [UIImage imageNamed:@"iTV.jpg"],
            [UIImage imageNamed:@"iRing.jpg"],
            [UIImage imageNamed:@"iwatch.jpeg"],
            [UIImage imageNamed:@"iMove.jpg"],nil];

    lblArray = [NSMutableArray arrayWithObjects:@"iPhone",@"iPad",@"iPod",@"iTV",@"iRing",@"iWatch",@"iMove", nil];

    UINib *cellNib = [UINib nibWithNibName:@"customCell" bundle:nil];
    [collections registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"];


}

- (void)didReceiveMemoryWarning
{
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}


 //Create a Colletion view using UICollectionViewDataSource

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
 {
    static NSString *cellIdentifier = @"cvCell";
    customCell *cell = (customCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    cell.img_Collection.image = [imgArray objectAtIndex:indexPath.row];
    cell.lbl_Collection.text = [lblArray objectAtIndex:indexPath.row];
    return cell;
 }

注意:将 BEARKPOINT 放入 CollectionView 数据源和委托中 Methods.Don不要忘记放置断点。