【问题标题】:add UICollectionView as subview of UICollectionReusableView reloadData not work, what I miss?添加 UICollectionView 作为 UICollectionReusableView reloadData 的子视图不起作用,我想念什么?
【发布时间】:2017-08-22 16:45:12
【问题描述】:

我尝试在运行时将 UICollectionView 添加到我的自定义 UICollectionReusableView 中,在主窗体中我有一个 UICollectionView(名为 mainView)向用户显示一级菜单,当用户在 mainView 中选择单元格时,它将展开二级脚部分的菜单(并调用 FolderContentView::loadSubMenus 方法), 以下是我不会工作的代码,请帮助。

.h 文件

@interface FolderContentView : UICollectionReusableView<UICollectionViewDataSource,UICollectionViewDelegate>

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

@end

.m 文件 #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. 尝试从主线程重新加载数据,它不会工作。
  2. 将 bp 设置为 numberOfItemsInSection numberOfItemsInSection,而不是提示。
  3. 尝试将委托和数据源设置为我的 mainView 的控制器,也不起作用。

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionreusableview


    【解决方案1】:

    “reloadData 不起作用”实际上是什么意思?委托方法是正确运行还是什么也没发生?


    1.也许您应该检查代码“在运行时将 UICollectionView 添加到我的自定义 UICollectionReusableView”。

    2.也许你忘了做:

    [self addSubview:_collectionView];
    

    3.此代码中的'cell'不会为nil:

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

    4.不要这样做:

    [cell.contentView addSubview:lblText];
    

    改用 UICollectionViewCell 的子类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多