简体   繁体   English

集合视图IOS中的像元大小未更改(在Xamarin中实现)

[英]Cell size not changed in collection view IOS (Implemented in Xamarin)

I have a collection view that needs to have the cells fitting size to content. 我有一个收集视图,需要使单元格适合内容的大小。 I use sizeForItemAtIndexPath: to set size for each cell. 我使用sizeForItemAtIndexPath:设置每个单元格的大小。 It's fired and works great. 它被发射了并且效果很好。

Here is the problem what I am facing. 这是我面临的问题。 The data list for collection view is changed dynamically. 收集视图的数据列表是动态更改的。

  • collection view has only 1 item to show and shows it in correct cell size(let's say size1 ) 集合视图仅显示一项,并以正确的像元大小显示(假设为size1
  • next time collection view has 1 item to show again but it's different data. 下次收集视图中有1个项目需要再次显示,但是它是不同的数据。

In this case, the collection view doesn't show it in correct cell size. 在这种情况下,集合视图不会以正确的单元格大小显示它。 it shows in size1 I want to show next item in its own size but collection view keep original size. 它以size1显示,我想以其自己的尺寸显示下一个项目,但集合视图保持原始大小。 When item count is changed, sizeForItemAtIndexPath: is fired again. 更改项目计数后,再次触发sizeForItemAtIndexPath: When item count is same, it's not fired. 如果项目计数相同,则不会触发。

I want it to be fired everytime. 我希望它每次都被解雇。

I share some of my code. 我分享一些代码。

cllSelected.Source = new SelectedItemSource(this, _selectedItems);

Above code will be called whenever there is new _selectedItems 每当有新的_selectedItems时,都会调用上述代码

I implemented the UICollectionViewSource as following 我实现了如下的UICollectionViewSource

public class SelectedItemSource : UICollectionViewSource, IUICollectionViewDelegateFlowLayout
    {
        CatalogVC _ownerVC;
        SelectedCategoryList _selectedItems;

        public SelectedItemSource(CatalogVC ownerVC, SelectedCategoryList selectedItems)
        {
            _ownerVC = ownerVC;
            _selectedItems = selectedItems;
        }

        public override nint NumberOfSections(UICollectionView collectionView)
        {
            return 1;
        }

        public override nint GetItemsCount(UICollectionView collectionView, nint section)
        {
            return _selectedItems != null ? _selectedItems.Count : 0;
        }

        public override bool ShouldHighlightItem(UICollectionView collectionView, Foundation.NSIndexPath indexPath)
        {
            return true;
        }

        public override void ItemHighlighted(UICollectionView collectionView, Foundation.NSIndexPath indexPath)
        {

        }

        public override void ItemUnhighlighted(UICollectionView collectionView, Foundation.NSIndexPath indexPath)
        {
            _ownerVC.SelectedFilter_ItemClick(indexPath.Row);
        }

        public override UICollectionViewCell GetCell(UICollectionView collectionView, Foundation.NSIndexPath indexPath)
        {
            SelectedFilterCollectionCell cell = collectionView.DequeueReusableCell("SelectedFilterCollectionCell", indexPath) as SelectedFilterCollectionCell;
            cell.Layer.ShouldRasterize = true;
            cell.Layer.RasterizationScale = UIScreen.MainScreen.Scale;

            cell.BindDataToCell(_selectedItems[indexPath.Row]);

            return cell;
        }

        [Export("collectionView:layout:sizeForItemAtIndexPath:")]
        public CoreGraphics.CGSize GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, Foundation.NSIndexPath indexPath)
        {
            var text = new NSString(_selectedItems[indexPath.Row].Text);
            CGSize cellSize = new CGSize(xxx, yyy); // cellSize will be determined according to text

            return cellSize;
        }
    }

Thanks for any solution or suggestion! 感谢您的任何解决方案或建议!

Try to call CollectionView.ReloadData() after the data changes. 数据更改后,尝试调用CollectionView.ReloadData()

I think it will invoke the methods included in the delegate. 我认为它将调用委托中包含的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM