【问题标题】:iOS custom grid view for iPad App to show downloaded images用于 iPad App 的 iOS 自定义网格视图以显示下载的图像
【发布时间】:2014-03-22 09:23:49
【问题描述】:

添加一个集合视图类型的对象,然后通过集合检查器建立正确的连接。

在.h文件中

IBOutlet UICollectionView   *myCollection;

在viewdidload中

 myCollection.delegate = self;
myCollection.dataSource = self;

数据源/委托方法是:

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

 // The cell that is returned must be retrieved from a call to -  dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell * cell = [collectionView  dequeueReusableCellWithReuseIdentifier:@"CellID" forIndexPath:indexPath];

cell.backgroundColor=[UIColor greenColor];

return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *  )indexPath
{
return CGSizeMake(100, 100);
}

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:  (NSIndexPath *)indexPath
{

}

收到此错误

could not dequeue a view of kind: UICollectionElementKindCell with identifier CellID -   must register a nib or a class for the identifier or connect a prototype cell in a   storyboard'

我现在该怎么办...请帮我解决它..

【问题讨论】:

  • 您有没有尝试过任何问题?
  • 你是否设置为像 [collectionView registerClass:[HACollectionCell class] forCellWithReuseIdentifier:@"CellID"];?
  • @Natarajan 感谢您对我的支持……但请告诉我在哪里设置它
  • @Natarajan 是的,我做到了……谢谢……只需在 viewdidload [myCollection registerClass:[DetailInvoicing2 class] forCellWithReuseIdentifier:@"CellID"] 中添加这一行;

标签: ios iphone objective-c ipad uicollectionview


【解决方案1】:

您可以使用UICollectionView 在 iOS 中显示网格视图。

例如:

在 HAViewController.h 中

#import <UIKit/UIKit.h>

@interface HACollectionCell : UICollectionViewCell

@end

@interface HAViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate>

@end

在 HAViewController.m 中

#import "HAViewController.h"    

@implementation HACollectionCell


@end

@implementation HAViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.width) collectionViewLayout:layout];

    collectionView.delegate = self;

    collectionView.dataSource = self;

    [collectionView registerClass:[HACollectionCell class] forCellWithReuseIdentifier:@"CellID"];

    [self.view addSubview:collectionView];

    collectionView = nil;
}

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

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

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellID" forIndexPath:indexPath];

    cell.backgroundColor=[UIColor greenColor];

    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(100, 100);
}

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{

}

@end

谢谢!

【讨论】:

  • @interface HACollectionCell : UICollectionViewCell @end ...。请你告诉我这里是什么意思。如何以及为什么包含它
  • 它将用于将您的自定义集合单元注册到 UICollectionView。例如:[collectionView registerClass:[HACollectionCell class] forCellWithReuseIdentifier:@"CellID"];
  • 您可以在 HACollectionCell 中进行更多自定义。它将与 UITableView 自定义单元格相同。
  • 请检查我编辑的问题……我已经尝试过,但遇到了一些例外……:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-16
  • 2016-11-25
相关资源
最近更新 更多