首页 > iOS 大神们进来看看这是什么问题,折腾了一个上午了

iOS 大神们进来看看这是什么问题,折腾了一个上午了

问题概述

在此先说明一下我的情况,为了减少ViewController的代码量,我将CollectionView的datasource抽象成独立的类。
头文件代码如下:

@interface CollectionViewAdapter : NSObject <UICollectionViewDataSource>

@property (nonatomic,copy) NSArray * identifiers;
@property (nonatomic,copy) NSArray * managers;
@property (nonatomic,retain) UICollectionView * collectionView;


@property (nonatomic,assign) NSInteger status;
@property (nonatomic,assign) BOOL statusSwitch;

@property (nonatomic,retain) id <AdapterDelegate> delegate;

-  (instancetype)initWithIdentifier:(NSArray *)identifiers withCollectionView:(UICollectionView *)collectionView withManager:(NSArray *)manager withStatus:(BOOL)openStatus;

- (instancetype)initWithIdentifier:(NSArray *)identifiers withCollectionView:(UICollectionView *)collectionView withManager:(NSArray *)manager;

@end

为了适配多个section,构造函数传的都是数组形式。
这个类的作用就是通过传进来的identifiers(这里我使用的identifier和cell的名字一致,所以没有传cell)、collectionView进行类的注册,并且实现DataSource里面的方法。(Manager是我自己设计的架构使用的一种获取数据的方式。)
构造函数具体实现如下:

- (instancetype)initWithIdentifier:(NSArray *)identifiers withCollectionView:(UICollectionView *)collectionView withManager:(NSArray *)manager
{
    self = [super init];
    if (self) {
        self.managers = manager;
        [self onCreateWithIdentifier:identifiers withCollectionView:collectionView];
    }
    return self;
}

- (void)onCreateWithIdentifier:(NSArray *)identifiers withCollectionView:(UICollectionView *)collectionView
{
    self.collectionView = collectionView;
    self.identifiers = identifiers;
    self.collectionView.dataSource = self;
    for (int i = 0;i<identifiers.count; i++) {
        [self.collectionView registerNib:[UINib nibWithNibName:self.identifiers[i] bundle:nil] forCellWithReuseIdentifier:self.identifiers[i]];
        if(self.statusSwitch) {
            [[ManagerFactory getManager:self.managers[i] withStatus:self.status] refresh:^()
             {
                 [self.collectionView reloadData];
                 //这里加载数据,刷新
             }];
        }else {
            [[ManagerFactory getManager:self.managers[i] withStatus:self.status] refresh:^()
             {
                 [self.collectionView reloadData];
             }];
        }
    }
}

dataSource的具体实现如下:


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.identifiers.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if(self.statusSwitch) {
        DLog("[[ManagerFactory getManager:self.managers[section] withStatus:self.status] getItems].count = %ld",
             [[ManagerFactory getManager:self.managers[section] withStatus:self.status] getItems].count);
        return [[ManagerFactory getManager:self.managers[section] withStatus:self.status] getItems].count;
    }else {
        return [[ManagerFactory getManager:self.managers[section]] getItems].count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = indexPath.section;
    Jastor * item;
    if(self.statusSwitch)
    {
        item = [[ManagerFactory getManager:self.managers[section] withStatus:self.status] getItemByIndex:indexPath.row];
    } else {
        item = [[ManagerFactory getManager:self.managers[section]] getItemByIndex:indexPath.row];
    }
    UICollectionViewCell * cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:self.identifiers[section] forIndexPath:indexPath];
    if(self.delegate&&[self.delegate respondsToSelector:@selector(cellAdapter:withCell:withSection:)])
    {
        cell = [self.delegate cellAdapter:item withCell:cell withSection:section];
        DLog("cell = %@",cell);
        //为了确定cell是否已经成功添加数据,在这里输出了一下
    }
    return cell;
}

其中的delegate用来通过viewController将具体的数据和view绑定在一起设计如下:

@protocol AdapterDelegate <NSObject>

- (UICollectionViewCell *)cellAdapter:(Jastor *)item withCell:(UICollectionViewCell *)cell withSection:(NSInteger)section;

@end

在viewController实现AdapterDelegate协议如下:

- (UICollectionViewCell *)cellAdapter:(Jastor *)item withCell:(UICollectionViewCell *)cell withSection:(NSInteger)section
{
    GoodsCell * temp  = (GoodsCell*)cell;
    StoreProductInfo * tempItem = (StoreProductInfo*)item;
    [temp.imageView sd_setImageWithURL:[NSURL URLWithString:tempItem.ShowImg]];
    [temp.nameView setText:tempItem.Name];
    [temp.priceView setText:[NSString stringWithFormat:@"¥%.2f",tempItem.ShopPrice]];
    [temp.oldPriceView setText:[NSString stringWithFormat:@"¥%.2f",tempItem.MarketPrice]];
    return temp;
}

目前情况

  1. 我能保证[self.collectionView reloadData]是在UI线程调用,并且有被调用的。

  2. 我能保证数据是有正确加载进去的。

  3. 我换用[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];结果有刷新,数据还没加载到控件上就一闪而过了。

objc不是很熟悉,求各路大神指点

【热门文章】
【热门文章】