Ios 无法调用UICollectionView的cellForItemAtIndexPath(不使用情节提要)
我是IOS开发新手,我尝试使用Ios 无法调用UICollectionView的cellForItemAtIndexPath(不使用情节提要),ios,objective-c,uicollectionview,uicollectionviewcell,Ios,Objective C,Uicollectionview,Uicollectionviewcell,我是IOS开发新手,我尝试使用UIcollectionView来显示照片 我没有使用情节提要 我创建一个xib文件调用AITFileCell\u grid.xib,并添加一个集合视图单元格,如下图所示 并创建另一个xib文件调用AITLocalGridViewController.xib,用于将UICollectionView添加到视图中 调用reloadData时,节numberOfItemsInSection的返回值大于1。 但是不能调用cellForItemAtIndexPath 我已经
UIcollectionView
来显示照片
我没有使用情节提要
我创建一个xib文件
调用AITFileCell\u grid.xib
,并添加一个集合视图单元格
,如下图所示
并创建另一个xib文件
调用AITLocalGridViewController.xib
,用于将UICollectionView
添加到视图中
调用reloadData
时,节numberOfItemsInSection
的返回值大于1。
但是不能调用cellForItemAtIndexPath
我已经设置了数据源
和委托
,如下图所示。
---------------------------------------编辑-------------------------------------
我添加了AITFileCell\u grid.xib的标识符,如下图所示
以下代码位于AITLocalGridViewController.h中
#import <UIKit/UIKit.h>
#import "AITFileCell_grid.h"
@interface AITLocalGridViewController : UICollectionViewController <UICollectionViewDataSource,UICollectionViewDelegate>
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@end
#import "AITLocalGridViewController.h"
#import "AITFileCell_grid.h"
#import "VLCMovieViewController.h"
@interface AITLocalGridViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
NSString *directory ;
NSArray *fileList ;
NSIndexPath *selectedIndexPath ;
UIDocumentInteractionController *documentInteractionController ;
}
@end
@implementation AITLocalGridViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (NSArray *)getFileList
{
NSLog(@"getFileList...@@@");
if (directory) {
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directory error:NULL];
return directoryContent;
}
return [[NSArray alloc] init] ;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"viewDidLoad...@");
// Do any additional setup after loading the view from its nib.
[self.collectionView registerClass:[AITFileCell_grid class] forCellWithReuseIdentifier:@"AITFileCell_grid"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if([paths count] > 0) {
directory = [paths objectAtIndex:0] ;
}
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated] ;
fileList = [self getFileList] ;
NSLog(@"viewDidAppear...fileList@@...%@" , fileList);
[self.collectionView reloadData];
[AITFileCell_grid startThumbnailFetching] ;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
NSLog(@"didReceiveMemoryWarning...@@@@");
// Dispose of any resources that can be recreated.
}
#pragma mark - Collection view data source
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
NSLog(@"numberOfSectionsInCollectionView...@");
// Return the number of sections.
return 1;
}
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSLog(@"numberOfItemsInSection...@...%lu", (unsigned long)[fileList count]);
// Return the number of rows in the section.
return [fileList count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"cellForItemAtIndexPath...@@");
// AITFileCell_grid *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[AITFileCell_grid reuseIdentifier] forIndexPath:indexPath];
AITFileCell_grid *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AITFileCell_grid" forIndexPath:indexPath];
// Configure the cell...
cell.fileName.text = [fileList objectAtIndex:indexPath.row] ;
NSDictionary * fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:@"%@/%@", directory, [fileList objectAtIndex:indexPath.row]] error:nil] ;
double fileSize = [fileAttributes fileSize] ;
NSString *sizeString = @"0" ;
if (fileSize < 1024) {
sizeString = [NSString stringWithFormat:@"%.2f", fileSize] ;
} else {
fileSize /= 1024 ;
if (fileSize < 1024) {
sizeString = [NSString stringWithFormat:@"%.2fKB", fileSize] ;
} else {
fileSize /= 1024 ;
if (fileSize < 1024) {
sizeString = [NSString stringWithFormat:@"%.2fMB", fileSize] ;
} else {
fileSize /= 1024 ;
if (fileSize < 1024) {
sizeString = [NSString stringWithFormat:@"%.2fGB", fileSize] ;
} else {
}
}
}
}
cell.fileSize.text = sizeString ;
NSDateFormatter *form = [[NSDateFormatter alloc] init];
[form setDateFormat:@"yyyy-MM-dd HH:mm"] ;
cell.filePath = [NSURL fileURLWithPath: [NSString stringWithFormat:@"%@/%@", directory, [fileList objectAtIndex:indexPath.row]]] ;
[AITFileCell_grid fetchThumbnail:cell] ;
return cell;
}
@end
我已将数据源
和委托
,以及numberOfItemsInSection
的值返回设置为大于1。
****但是在调用了numberofitemsin节
之后,不能调用cellForItemAtIndexPath
**
我不确定AITFileCell\u网格是否已注册…
但它不会在单元格上显示单元格。
有人能帮我并教我怎么做吗?首先,您需要将UICollectionViewDataSource协议添加到controller@界面
然后,将集合视图单元格添加到coll。看法
另外,不要忘记添加重用标识符:
你可以用“手机”
cellForRowAtIndexPath:
是一种方法。用于表视图,而不是集合视图。如果使用的是集合视图,则应实现的方法,并将该对象设置为集合视图的数据源。我对tableview和collectionview都有相同的问题,有时方法registerClass不起作用。您是否尝试加载nib文件,然后进行注册?它应该会起作用
UINib *nib = [UINib nibWithNibName:@"AITFileCell_grid" bundle:nil];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:@"AITFileCell_grid"];
只需从侧菜单添加集合视图单元格…如果我已经编写了[self.collectionView registerNib:nib forCellWithReuseIdentifier:[AITFileCell\u grid reuseIdentifier]],是否应该添加重用标识符代码>?当然,您应该这样做,因为此属性使用图片上字段的值我已经为AITLocalGridViewController.m
添加了代码,它已经为控制器@接口添加了协议。我为重用标识符添加了FileCell
。但是,UICollectionView
的cellForItemAtIndexPath
仍然无法调用…您可以将项目发送给我吗mvadim@me.com我试着检查出了什么问题对不起…我弄错了。它是cellForItemAtIndexPath
而不是cellforrowtindexpath
。将该对象设置为集合视图的数据源是什么意思?这和我贴的照片一样吗?对不起,你知道吗?对不起,我还没有看完你的问题。你的自定义手机可能没有问题。该方法是否真的不能调用collectionView:cellForItemAtIndexPath:?只有两个原因:-返回的数字部分=0-返回的单元格数=0可以打印并显示文件列表数组的日志。