【问题标题】:How to skip a cell / return no cell from GetCell method, UICollectionView如何从 GetCell 方法 UICollectionView 跳过单元格/不返回单元格
【发布时间】:2017-03-15 23:02:27
【问题描述】:

我有一个“TagInstances”列表,每个都包含一个“颜色”字符串值。

List<TagInstance> tags

在我的 UICollectionView 中,如果:

tags[index.Path].color = "undefined"

我试图在“cellForItemAtIndexPath”/“GetCell”方法中执行此操作:

[Export("collectionView:cellForItemAtIndexPath:")]
public UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
    var cell = (TagCell)collectionView.DequeueReusableCell("tagCell", indexPath);

    var thisTag = tags[indexPath.Row];

    if (thisTag.color == "undefined") { /* todo: Skip over this cell somehow */ }

    try
    {
        var color = UIColor.Clear.FromHex(thisTag.color);

        cell.SetUIWithData(color, thisTag.abbreviation);

        cell.AddBorderAndShadow();
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine($"\ncolor : { thisTag.color }\nThrows an exception : \n{ ex }\n");
    }

    return cell;
}

目前,我的集合视图显示有任何 TagInstance 具有和未定义颜色的间隙。我希望这个系列的出现没有那个间隙。这可能吗?

【问题讨论】:

  • 正确的解决方案是不在您的集合视图数据源中包含这些值。
  • 分配一个排除undefined元素的过滤数据集..

标签: ios xamarin xamarin.ios uicollectionview


【解决方案1】:

正如 rmaddy 和 SushiHangover 所说,正确的方法是拥有正确的数据源,其中只有您想要显示的项目。

在某种程度上,替代方法是将那些不需要的单元格调整为宽度/高度 = 0。要使用 UICollectionView 执行此操作,您必须从 UICollectionViewFlowLayout 派生并覆盖 LayoutAttributesForElementsInRect 方法,然后为您返回的那些元素返回 Size = CGSize.Empty不想看。

请参阅Xamarin Docs in Introduction to UICollectionView 中的子类化 UICollectionViewFlowLayout 部分。

同样,正确且更简单的方法是按照 SushiHangover 和 rmaddy 的建议,在数据源级别过滤元素。

【讨论】:

    【解决方案2】:

    1) 从您不想在 collectionview 中显示单元格的数据源中删除该数据。

    tags.RemoveAll(p => p.color== "undefined" );
    

    2) 如果您出于其他原因想要将该数据保留在列表中,请使用 UICollectionViewDelegateFlowLayout 的 GetSizeForItem 方法。

    在 UICollectionviewSource 中使用以下内容

    [Export ("collectionView:layout:sizeForItemAtIndexPath:"), CompilerGenerated]
    public virtual CGSize GetSizeForItem (UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
    {
    var thisTag = tags[indexPath.Row];
    
    if (thisTag.color == "undefined")
            return new CGSize(0,0);
    else 
        return new CGSize (yoruwidth, yourheigh);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-02
      • 1970-01-01
      • 2023-03-27
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多