Search code examples
ioslistgridcellscollectionview

iOS collectionView from grid to list with 2 prototypes cells


I want to make a collection with 2 styles : Grid and List.

So, at the moment, I have :

  • 1 collection View
  • 2 flow Layout
  • 1 dataSource
  • 2 prototypes cells

I have to use the same datasource. I can switch between layout, it's not a problem.

But my GetCell method in my DataSource returns a cell from only 1 prototype. So when I switch, disposition cells are change but not their content.

GetCellMethod :

public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
    {
        Transaction transaction = Transactions[indexPath.Row];

        var cell = (TransactionCellList)collectionView.DequeueReusableCell("transactionList", indexPath);

        cell.UpdateRow(transaction);

        return cell;

    }

How can I make a difference between these 2 prototypes cells using a switch button grid / list ??

Thanks a lot for help !!!

PS : Image here on stack post : Display items in different styles(List, Grid, Blocks) in Xamarin ios?


Solution

  • You can use the .isSelected property of UIButton to switch between the cells

    public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
        {
    Transaction transaction = Transactions[indexPath.Row];
    if switch.isSelected {
     var cell = (TransactionCellList)collectionView.DequeueReusableCell("transactionList", indexPath);
    
            cell.UpdateRow(transaction);
    
            return cell;
    
    }
            else{
       var cell = (TransactionCellList)collectionView.DequeueReusableCell("transactionGrid", indexPath);
    
            cell.UpdateRow(transaction);
    
            return cell;
    }
    
    
     }
    

    In IBAction of the switch button you can use switch.isSelected. = !switch.isSelected