UICollectionView数据源方法未被调用,但其委托方法被调用

时间:2016-09-22 16:03:03

标签: ios objective-c uicollectionview uicollectionviewcell

接口文件是, 我为uicollectionview添加了委托和数据源。

@interface HomeVC ()  <UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
    UITableView *_hotTable;
    NSArray *_hotArray;
}

@end

以下是实施文件

@implementation HomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initScrollView];
    [self initHotView];
}

#pragma mark - init ScrollView
-(void)initScrollView{
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-64-44)];
    _scrollView.contentSize = CGSizeMake(1*kScreenW, _scrollView.frame.size.height);
    _scrollView.backgroundColor = [UIColor  redColor];
   _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    [self.view addSubview:_scrollView];   
}
#pragma mark - init hot view
-(void)initHotView{
    _hotTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, _scrollView.frame.size.height) style:UITableViewStylePlain];
    _hotTable.delegate = self;
    _hotTable.dataSource = self;
    _hotTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [_hotTable registerNib:[UINib nibWithNibName:@"HotTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kCellReuseId];
    [_scrollView addSubview:_hotTable];

}

#pragma mark - request api
-(void)requestHot{
    [AFAPIRequest getOneLevelLaber:@"1" requestCallBack:^(id result) {
        if ([ResponseTool responseIsSuccessed:result]) {
          _hotArray = [ParentLaberModel initModelArraytWithDictionaryArray:result[@"info"]];
              if (_hotArray.count >0) {

                for (ParentLaberModel *model in _hotArray) {
                    [AFAPIRequest getTwoLevelLaber:model.laberParentId requestCallBack:^(id result) {
                        if ([ResponseTool responseIsSuccessed:result]) {

                            model.laberArray = [LaberModel initModelArraytWithDictionaryArray:result[@"info"]];

                            [TipTool hideTipView];
                        }else{
                            [TipTool showErrorTip:[ResponseTool responseMsg:result]];
                        }
                    }];
                }
                     [_hotTable reloadData];
            }
        }else{
            [TipTool showErrorTip:[ResponseTool responseMsg:result]];
        }

    }];

}

#pragma mark - tableview  delegate datasource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return _hotArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = kCellReuseId;

        HotTableCell *cell  = [tableView dequeueReusableCellWithIdentifier:cellId];
        [cell setData:_hotArray[indexPath.row]];
         return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 160;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

        NSLog(@"**********");

        HotTableCell *hotTableCell = (HotTableCell *)cell;
        [hotTableCell setCollectionViewDataSourceDelegate:self indexPath:indexPath];

   }

#pragma mark - UICollectionView  delegate datasource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSLog(@"111111");
    HotTableCollectionView *hotTableCollectionView = (HotTableCollectionView*)collectionView;
  NSLog(@"%@",collectionView.dataSource);
    return [[_hotArray[hotTableCollectionView.indexPath.row] laberArray] count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HotTableCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellReuseId forIndexPath:indexPath];
    NSLog(@"---------------------------");
    HotTableCollectionView *hotTableCollectionView = (HotTableCollectionView*)collectionView;
    [cell setCellData: [_hotArray[hotTableCollectionView.indexPath.row] laberArray][indexPath.row]];
    return cell;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    NSLog(@"222222");
    collectionView.backgroundColor = [UIColor blueColor];
     NSLog(@"%@",collectionView.dataSource);
    return 0;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    NSLog(@"333333");
     NSLog(@"%@",collectionView.dataSource);
    return 0;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"444444");
    return CGSizeMake(100, 120);
}@end

因此,不会调用上面代码中的数据源方法。

0 个答案:

没有答案