3

嗨,我已经创建了一个,我用 CustomCellUIViewController添加了一个。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;

}

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

4

4 回答 4

22

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

在情节提要中,我们不需要这个

于 2013-11-07T08:11:56.613 回答
1

代码没问题,但顺序不同。在填充数组之前分配数据源和委托会使单元格的数量返回 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>类头中有

于 2013-11-07T07:05:31.117 回答
0
  1. 在您的故事板中,给您imageview一个tag例如 1,而您的label另一个tag例如 2

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

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

  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)section {

            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;
      
                }
      
于 2013-11-07T07:28:22.697 回答
-1
- (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;

}
于 2013-11-07T07:21:23.073 回答