Pinterest样式布局iOS

Pinterest样式布局iOS

我想创建一个像这样的设计:

ios - Pinterest样式布局iOS Storyboard-LMLPHP

我正在使用情节提要,但我无法使此功能正常工作。到目前为止,我有一个简单的集合视图,除了所有单元格都显示两次之外,它都可以正常工作。我有没有办法通过不使用任何库并为单元格设置随机高度来实现此目的?

这是我的故事板:-

ios - Pinterest样式布局iOS Storyboard-LMLPHP

Mycollectioncontroller

public MyCollectionController(IntPtr handle) : base(handle)
{

}

public override void ViewDidLoad ()
{
    base.ViewDidLoad ();
    data = MakeDataForRecyclerView(pageIndex);
}

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

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

public override void ItemSelected (UICollectionView collectionView, NSIndexPath indexPath)
{
    var message = string.Format ("You clicked on {0}!", cell.Title);
    new UIAlertView ("Clicked", message, null, "Okay", null).Show ();
}

public override UICollectionViewCell GetCell (UICollectionView collectionView, NSIndexPath indexPath)
{
    cell = (MyCollectionCell) collectionView.DequeueReusableCell ("My_Collection_Cell", indexPath);
    var tag = data[indexPath.Row];
    cell.PopulateData (tag.Title, tag.ThumbnailPath);
    return cell;
}

public override void ItemHighlighted(UICollectionView collectionView, NSIndexPath indexPath)
{
    var cell = collectionView.CellForItem(indexPath);
    cell.ContentView.BackgroundColor = UIColor.Yellow;
}

public override void ItemUnhighlighted(UICollectionView collectionView, NSIndexPath indexPath)
{
    var cell = collectionView.CellForItem(indexPath);
    cell.ContentView.BackgroundColor = UIColor.White;
}

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

private Models.DummyModel[] MakeDataForRecyclerView(int startPosition)
{
    List<Models.DummyModel> listOf4Item = new List<Models.DummyModel>();

    for (int i = startPosition * 4; i < Math.Min((4 * startPosition) + 4, AppDelegate.myList.Count); i++)
    {
        listOf4Item.Add(AppDelegate.myList.ElementAt(i));
    }

    return listOf4Item.ToArray();
}

MyCollectionCell
public MyCollectionCell (IntPtr handle) : base (handle)
{

}

public void PopulateData(string title, string placeholderURL)
{
    //lblTitle.Text = title;
    placeholderImg.SetImage(url: new NSUrl(placeholderURL),
                            placeholder: UIImage.FromFile("ic_apptitle.png"));
}

最佳答案

您的问题在这里:

public override nint NumberOfSections (UICollectionView collectionView)
{
    return 2; // should be 1 not 2
}

要为您的单元格设置不同的大小,您可以添加以下方法:
public override SizeF GetSizeForItem (UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
{
    // return the size you want for the cell at this indexPath
}

关于ios - Pinterest样式布局iOS Storyboard,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35033430/

10-11 09:23