gpt4 book ai didi

iPad 应用程序的 iOS 自定义 GridView 显示下载的图像

转载 作者:行者123 更新时间:2023-11-29 03:07:44 26 4
gpt4 key购买 nike

I want to make this type of view in my app.

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

在.h文件中

IBOutlet UICollectionView   *myCollection;

在viewdidload中

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

数据源/委托(delegate)方法是:

- (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'

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

最佳答案

您可以使用 UICollectionView 在 iOS 中显示 GridView 。

例如:

在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

谢谢!

关于iPad 应用程序的 iOS 自定义 GridView 显示下载的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22575735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com