使用 collectionView 和 collectionViewCells

Posted

技术标签:

【中文标题】使用 collectionView 和 collectionViewCells【英文标题】:Working with collectionView and collectionViewCells 【发布时间】:2016-04-02 11:53:59 【问题描述】:

这是我第一次使用 UICollectionView 和 UICollectionViewCells。我正在尝试做一个简单的视图。我面临的问题是:重新加载集合视图数据,将文本(从数组)显示到单元格标签中。 代码在这里.. 请尽可能提供帮助..

  #import "MainCollectionViewController.h"
#import "MainCollectionViewCell.h"


@interface MainCollectionViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>


    UICollectionView *colView;

@end

@implementation MainCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad 
    [super viewDidLoad];


 //  [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];


    self.actressArray = [NSMutableArray arrayWithObjects:@"John", @"Patrick", @"Jack", @"Clara", @"Nicol", @"Amanda", @"Nick", @"Bob", @"Jeff", @"Jerremy", @"Gloria", @"Bred", @"Seth", @"Peter", @"Lous", nil];



    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
    colView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    [colView setDataSource:self]; //დელეგატების კოდით ჩართვა
    [colView setDelegate:self];

    [colView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    [colView setBackgroundColor:[UIColor grayColor]]; //ვიუს ბექგრაუნდის ფერი

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    [self.collectionView setCollectionViewLayout:flowLayout];

    [self.view addSubview:colView]; //ვიუზე ჩვენ მიერ გამზადებული ვიუს დაგდება

    [self.collectionView registerClass:[MainCollectionViewCell class] forCellWithReuseIdentifier: reuseIdentifier];


    ////[self.collectionView reloadData];



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





//// In a storyboard-based application, you will often want to do a little preparation before navigation
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender //
//




- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 

    return _actressArray.count;



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 


    return 3;


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 

    MainCollectionViewCell *cell = (MainCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];



    NSString *actressStr = [_actressArray objectAtIndex:indexPath.row];
    cell.actressLabel.text = actressStr;

    //[self.collectionView reloadData];

    //[cell.contentView addSubview:  ];



    return cell;

【问题讨论】:

【参考方案1】:

1请再次检查您的网点连接:-

2还有什么错误???

【讨论】:

我已经解决了这个问题,但强制新的。重新加载数据不起作用 在cellForItemAtIndexPath中不需要重新加载collectionview 我在 viewDidLoad viewWillAppear 中尝试过.. 但不起作用 link 试试这个链接 从开始appcoda.com/ios-programming-uicollectionview-tutorial 重新开始 或者===>>***.com/questions/17856055/…【参考方案2】:

这里是更新的。重新加载数据不起作用

#import "MainCollectionViewController.h"
#import "MainCollectionViewCell.h"


@interface MainCollectionViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>


    UICollectionView *colView;

@end

@implementation MainCollectionViewController

static NSString * const reuseIdentifier = @"Cell";





- (void)viewDidLoad 
    [super viewDidLoad];


 //  [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];


    self.actressArray = [NSMutableArray arrayWithObjects:@"John", @"Patrick", @"Jack", @"Clara", @"Nicol", @"Amanda", @"Nick", @"Bob", @"Jeff", @"Jerremy", @"Gloria", @"Bred", @"Seth", @"Peter", @"Lous", nil];



    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
    colView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    [colView setDataSource:self]; //დელეგატების კოდით ჩართვა
    [colView setDelegate:self];

    [colView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    [colView setBackgroundColor:[UIColor grayColor]]; //ვიუს ბექგრაუნდის ფერი

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    [self.collectionView setCollectionViewLayout:flowLayout];

    [self.view addSubview:colView]; //ვიუზე ჩვენ მიერ გამზადებული ვიუს დაგდება

    [self.collectionView registerClass:[MainCollectionViewCell class] forCellWithReuseIdentifier: reuseIdentifier];

    [colView reloadData];





//// In a storyboard-based application, you will often want to do a little preparation before navigation
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
//



- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 

    return 2;



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 


    return _actressArray.count;


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 



    MainCollectionViewCell *cell = (MainCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    cell.backgroundColor = [UIColor whiteColor];
    cell.layer.cornerRadius = 10;
    cell.layer.borderWidth = 2.0;
    cell.layer.borderColor = [[UIColor lightGrayColor] CGColor];

    UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, cell.bounds.size.width -20 , cell.bounds.size.height)];
    testLabel.text = _actressArray[indexPath.row];
    testLabel.font = [UIFont fontWithName:@"Helvetica" size:12];
    testLabel.textAlignment = NSTextAlignmentCenter;
    testLabel.lineBreakMode = NSLineBreakByWordWrapping;
    testLabel.numberOfLines = 0;

    [cell.contentView addSubview:testLabel];

    return cell;




- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

    return CGSizeMake(collectionView.frame.size.width/3.2 , 100);
 //ხაზზე სამისთვის

【讨论】:

对于初学者来说是从 UICollectionViewController 扩展而来的。在 viewDidLoad() 中也不需要 reloadData,reloadData 仅在您的数据发生更改时使用。

以上是关于使用 collectionView 和 collectionViewCells的主要内容,如果未能解决你的问题,请参考以下文章

当scrollview快速滚动时如何滚动collectionView?

当键盘出现时 CollectionView 向上移动然后返回到之前的位置

自定义 CollectionView 崩溃 iOS

CollectionView 与单元格重叠

如果collectionView低于某些uiview,iOS如何使collectionView接收触摸

iOS - 在视图(Swift)中为多个Tableview或Collectionviews使用/传递手势识别器