Luda
Luda

Reputation: 7068

Proper way to reuse UITableViewCell containing UICollectionView

I have a UITableView. Each cell contains horizontal collection view. When I scroll the table, collection view contains too much cells. I guess it is because the cells are not properly reused. In the illustration, the grey cells were not suppose to be there. table view

What is the proper code that I should put in the reuseCell? I tried the following but it made the app crash

over

ride func prepareForReuse()
    {
        super.prepareForReuse()
        channelsCollectionView = UICollectionView()
    }

Upvotes: 1

Views: 1495

Answers (2)

rushisangani
rushisangani

Reputation: 3385

Please follow the below steps. That will work perfectly fine CollectionView inside UITableViewCell.

  1. Make subclass of UICollectionView like this.

    @interface MyCollectionView : UICollectionView <UICollectionViewDataSource, UICollectionViewDelegate>
    
    /* Properties */
    @property (nonatomic, strong)  NSMutableArray *imagesArray;
    
    @end
    
  2. In your MyCollectionView.m file

    @interface MyCollectionView ()
    
    @property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
    
    @end
    
    @implementation MyCollectionView
    
    -(void)awakeFromNib {
    
        [super awakeFromNib];
    
        self.dataSource = self;
        self.delegate = self;
    
        //Set CollectionView Layout
        [self setCollectionViewLayout:self.flowLayout];
    }
    
    #pragma mark - CollectionView DataSource methods
    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
       return 1;
    }
    
    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
       return _imagesArray.count;
    }
    
    
    -(UICollectionViewFlowLayout *)flowLayout{
    
      // set your layout here
     _flowLayout = [[UICollectionViewFlowLayout alloc] init];
     [_flowLayout setSectionInset:UIEdgeInsetsMake(topSpacing, leftMargin, bottomSpacing, rightMargin)];
     [_flowLayout setMinimumInteritemSpacing:cellSpacing];
     [_flowLayout setMinimumLineSpacing:lineSpacing];
     [_flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
     [_flowLayout setItemSize:CGSizeMake(cellWidth, cellHeight)];
    
     return _flowLayout;
    }
    
    #pragma mark - setter method
    -(void)setImagesArray:(NSMutableArray *)imagesArray {
    
     _imagesArray = imagesArray;
      [self reloadData];
    
    }
    
    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
       // configure cell here
    
    }
    
  3. In your ViewController.m having UITableView

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
           MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier" forIndexPath:indexPath];
    
           cell.myCollectionView.imagesArray = //Array here
    
           return cell;
    
         }
    
  4. You need to set fix height of your collectionView in TableViewCell. As you've horizontal collectionView.

Upvotes: -1

Anil Varghese
Anil Varghese

Reputation: 42977

It should be

//Reset the datasource
channelsCollectionView.dataSourceArray = []()

//Reload data of collectionView
channelsCollectionView.reloadData()

Again its depends, this is one way of doing

Upvotes: 4

Related Questions