【发布时间】:2018-02-10 04:18:40
【问题描述】:
我有一个集合视图,需要使单元格大小适合内容。
我使用sizeForItemAtIndexPath: 为每个单元格设置大小。
它已被解雇并且效果很好。
这是我面临的问题。 集合视图的数据列表是动态变化的。
- 集合视图只有 1 项要显示并以正确的单元格大小显示(比如说 size1)
- 下次收集视图有 1 项要再次显示,但数据不同。
在这种情况下,集合视图不会以正确的单元格大小显示它。它以 size1 显示
我想以自己的大小显示下一个项目,但集合视图保持原始大小。
当项目计数发生变化时,sizeForItemAtIndexPath: 会再次被触发。
当物品数量相同时,它不会被触发。
我希望它每次都被解雇。
我分享我的一些代码。
cllSelected.Source = new SelectedItemSource(this, _selectedItems);
只要有新的_selectedItems,上面的代码就会被调用
我实现了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;
}
}
感谢任何解决方案或建议!
【问题讨论】:
-
分享你的代码
-
@ChandreshKachariya 分享了我的代码。
-
让我们尝试设置数据源并以编程方式委托
-
解决问题?
-
不,它不起作用。正如我所描述的,它运行良好,但在某些情况下不起作用。
标签: ios xamarin xamarin.ios uicollectionview