如何删除CollectionView IOS中行之间的额外行间距?

时间:2018-03-22 15:01:58

标签: c# ios objective-c iphone xamarin

enter image description here

我希望通过移除细胞顶部的额外空白来移动细胞,因为细胞具有不同的高度。我正在使用流布局,我实现了flowlayout的委托方法以及行和项间距。

这是我的代码

public partial class TestVc : UIViewController
{    
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        string[] words = { "one", "two", "three", "four", "five", 
            "six seven eight", "nine ten eleven twelve thirteen",
            "the quick brown fox"};

        grid.DataSource = new MyDS(words);
        grid.Delegate = new FlowLayoutDelegate(words);
        var layout = new UICollectionViewFlowLayout();
    }
}

public class MyDS : UICollectionViewDataSource
{
    string[] words;

    public MyDS(string[] _words)
    {
        words = _words;
    }

    public override UICollectionViewCell GetCell(UICollectionView collectionView, 
        NSIndexPath indexPath)
    {    
        UICollectionViewCell cell = collectionView
            .DequeueReusableCell("MyCellTest", indexPath) as UICollectionViewCell;
        MyCellTest demoCell = cell as MyCellTest;

        return cell;
    }

    public override nint GetItemsCount(UICollectionView collectionView, 
        nint section)
    {
        return words.Length;
    }

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

class FlowLayoutDelegate : UICollectionViewDelegateFlowLayout
{
    string[] items;

    public FlowLayoutDelegate(string[] items)
    {
        this.items = items;    
    }

    public override nfloat GetMinimumInteritemSpacingForSection(
        UICollectionView collectionView, UICollectionViewLayout layout, nint section)
    {
        return 5;
    }

    public override nfloat GetMinimumLineSpacingForSection(
        UICollectionView collectionView, UICollectionViewLayout layout, nint section)
    {
        return 5;
    }

    public override CoreGraphics.CGSize GetSizeForItem(UICollectionView collectionView, 
        UICollectionViewLayout layout, NSIndexPath indexPath)
    {
        int numOfcoll = 2;
        int margans = 40;

        if (indexPath.Row == 0)
        {
            return new CGSize((collectionView.Bounds.Width - margans) / numOfcoll, 200);
        }
        else
        {
            return new CGSize((collectionView.Bounds.Width - margans) / numOfcoll, 73);
        }
    }
}

请告诉我没有任何图书馆的任何解决方案。我已经发布了一个问题,但没有得到Xamarin社区的答复,所以我在其中添加了IOS社区作为其C#代码,但不要担心我在目标c中提出解决方案,我将在C#Xamarin由我自己。

1 个答案:

答案 0 :(得分:1)

您需要根据图像的大小计算每个图像的高度并设置sizeofcell。

您可以查看本教程。

https://www.raywenderlich.com/164608/uicollectionview-custom-layout-tutorial-pinterest-2