簡體   English   中英

不調用UICollectionView的cellForItemAtIndexPath(不使用情節提要)

[英]The cellForItemAtIndexPath for UICollectionView not be called (without using storyboard)

我是IOS開發的新手,我嘗試使用UIcollectionView來顯示照片。

我沒有使用storyboard

我創建了一個xib file調用AITFileCell_grid.xib ,並添加Collection View Cell類似下面的圖片。

在此處輸入圖片說明

並創建另一個xib file調用AITLocalGridViewController.xib添加UICollectionView進入View

當我調用reloadDatanumberOfItemsInSection的返回值大於1。但是未調用cellForItemAtIndexPath

我已經設置了dataSourcedelegate ,如下圖所示。 在此處輸入圖片說明

- - - - - - - - - - - - - - - - - - - - - 編輯 - - - - ---------------------------------------

我已經為AITFileCell_grid.xib添加了標識符,如下圖所示。

在此處輸入圖片說明

以下代碼在AITLocalGridViewController.h

#import <UIKit/UIKit.h>
#import "AITFileCell_grid.h"

@interface AITLocalGridViewController : UICollectionViewController <UICollectionViewDataSource,UICollectionViewDelegate>
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;

@end

以下代碼在AITLocalGridViewController.m

#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

我已經設置了dataSourcedelegate ,並且numberOfItemsInSection值返回大於1。

****但是在調用numberOfItemsInSection之后不會調用cellForItemAtIndexPath 。**

我不確定AITFileCell_grid是否已注冊...

但是它沒有在單元格上顯示該單元格。

有人可以幫助我,教我怎么做嗎?

首先,您需要將UICollectionViewDataSource協議添加到控制器@interface。

然后,將“收藏夾”視圖單元格添加到您的收藏夾。 視圖

在此處輸入圖片說明

另外,別忘了添加重用標識符:您可以使用ex。 “細胞”

識別碼

我在tableview和collectionview上都有相同的問題,有時方法registerClass不起作用。 您是否嘗試過加載nib文件然后注冊? 它應該工作。

UINib *nib = [UINib nibWithNibName:@"AITFileCell_grid" bundle:nil];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:@"AITFileCell_grid"];

cellForRowAtIndexPath:是一個UITableViewDataSource方法。 這是用於表視圖,而不是集合視圖。 如果使用集合視圖,則應實現UICollectionViewDataSource的方法,並將該對象設置為集合視圖的數據源。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM