0

I'm try to add UICollectionView to my custom UICollectionReusableView in runtime, in main form I have a UICollectionView(named mainView) to show first-level menus to user, when user did select cell in mainView it will expand second-level menus in foot section(and call FolderContentView::loadSubMenus method), following is my wont work code, please help.

.h file

@interface FolderContentView : UICollectionReusableView<UICollectionViewDataSource,UICollectionViewDelegate>

   -(void) loadSubMenus:(NSArray<EnabledModule*>*) subModules;

@end

.m file #import "FolderContentView.h"

@implementation FolderContentView {
    UICollectionView *_collectionView;
    NSArray* _subMenus;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(100, 100);
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        _collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }

    [_collectionView reloadData];

    return self;
}

-(void) loadSubMenus:(NSArray<EnabledModule*>*) subModules {
    if(subModules != nil) {
        _subMenus = [subModules copy];
    } else {
        _subMenus = nil;
    }
    [_collectionView reloadData];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _subMenus!=nil?_subMenus.count:0;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView
                                      dequeueReusableCellWithReuseIdentifier:@"cell"
                                      forIndexPath:indexPath];
    if(!cell){
        cell=[[UICollectionViewCell alloc]init];
    }

    EnabledModule *model = _subMenus[indexPath.row];

    cell.hidden = !model.enabled;
    UILabel *lblText = [[UILabel alloc] init];
    lblText.text = model.moduleName;
    [cell.contentView addSubview:lblText];

    return cell;
}

@end
  1. try reloadData from main thread, it's wont work.
  2. set bp to numberOfItemsInSection numberOfItemsInSection, not hint.
  3. try to set delegate and dataSource to my mainView's controller, not work too.
code0tt
  • 41
  • 8

1 Answers1

1

What's 'reloadData not work' actually means? Did the delegate methods run correctly or nothing happen?


1.Maybe you should check the codes 'add UICollectionView to my custom UICollectionReusableView in runtime'.

2.Maybe you forgot to do:

[self addSubview:_collectionView];

3.The 'cell' will not be nil in this code:

UICollectionViewCell *cell = [collectionView
                                  dequeueReusableCellWithReuseIdentifier:@"cell"
                                  forIndexPath:indexPath];
if(!cell){
    cell=[[UICollectionViewCell alloc]init];
}

4.Don't do this:

[cell.contentView addSubview:lblText];

Use subClass of UICollectionViewCell instead.

liftlift
  • 59
  • 4