未调用UICollectionView cellForItemAtIndexPath方法

时间:2015-07-29 09:38:15

标签: ios objective-c uicollectionview uicollectionviewcell

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;
}

4 个答案:

答案 0 :(得分:2)

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

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

很简单。

答案 1 :(得分:2)

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

答案 2 :(得分:2)

我完全给你编码并遵循它。你可以得到解决方案

in .h

#import <UIKit/UIKit.h>

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


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

@end

in .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;
 }

注意:在CollectionView DataSource和Delegates Methods中放置 BEARKPOINT 。不要忘记放置断点。

答案 3 :(得分:0)

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

.login
相关问题