OGeek|极客世界-中国程序员成长平台

标题: ios - 如何从 GetCell 方法 UICollectionView 跳过单元格/不返回单元格 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:02
标题: ios - 如何从 GetCell 方法 UICollectionView 跳过单元格/不返回单元格

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

List<TagInstance> tags

在我的 UICollectionView 中,如果出现以下情况,我想放弃 tagCells 的初始化:

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;
}

目前,我的 Collection View 显示有任何 TagInstance 具有未定义颜色的间隙。我希望该系列能够毫无间隙地出现。这可能吗?



Best Answer-推荐答案


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

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

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

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

关于ios - 如何从 GetCell 方法 UICollectionView 跳过单元格/不返回单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42822330/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4