I want to use a UICollectionView to show a serie of images that the user can choose from the gallery. So I implemented a UICollectionViewDataSource, however in the GetCell method there is a problem when IOS tries to reuse the cell. I can load into the collection view the first two images, when I load the third one, the first cell's content gets blank, the same thing happens to the second cell when I create the fourth one and so on.
Here is my code for the DataSource:
public MyCollectionDataSource(List<string> items)
{
this.items = items;
}
public override UICollectionViewCell GetCell (UICollectionView collectionView, NSIndexPath indexPath)
{
var cell = (PhotoCell)collectionView.DequeueReusableCell (new NSString ("PhotoCell"), indexPath);
cell.Image = UIImage.FromFile (items[indexPath.Row]);
return cell;
}
public override int NumberOfSections (UICollectionView collectionView)
{
return 1;
}
public override int GetItemsCount (UICollectionView collectionView, int section)
{
return items.Count;
}
I also Implemented UICollectionViewFlowLayout to give a linear layout to my collection view (I took this code from docs.xamarin.com/guides/ios/user_interface/introduction_to_collection_views/). This is how that looks:
public override UICollectionViewLayoutAttributes[] LayoutAttributesForElementsInRect (RectangleF rect)
{
var array = base.LayoutAttributesForElementsInRect (rect);
var visibleRect = new RectangleF (CollectionView.ContentOffset, CollectionView.Bounds.Size);
foreach (var attributes in array) {
if (attributes.Frame.IntersectsWith (rect)) {
float distance = visibleRect.GetMidX () - attributes.Center.X;
float normalizedDistance = distance / ACTIVE_DISTANCE;
if (Math.Abs (distance) < ACTIVE_DISTANCE) {
float zoom = 1 + ZOOM_FACTOR * (1 - Math.Abs (normalizedDistance));
attributes.Transform3D = CATransform3D.MakeScale (zoom, zoom, 1.0f);
attributes.ZIndex = 1;
}
}
}
return array;
}
And here is the code which adds a new string path to the data source list of items:
galleryPicker.TouchUpInside += async (sender, e) => {
var file = await picker.PickPhotoAsync();
items.Add(file.Path);
dataSource.items = items;
InvokeOnMainThread(() => {
photoCollection.ReloadData();
});
};
I'm open to suggestions!! Attached are 2 images which show whats happening, the first one is when I have only 2 cells created, the second one happens when I create a third cell!