Forum Xamarin.iOS

CollectionView Cell width is not changing on different screen sizes

Yasir.aliYasir.ali USMember

Hello All....
I am trying to change the cell size (width) in Get Cell method at runtime according to screen size and the problem is that content size of CollectionView remains same as the {(size of cell is defined on StoryBoard) * (Cell Count)} .....

Any Help..............

public class CollectionViewSource : UICollectionViewSource
{
public CollectionViewSource(List imageNames, CGSize size)
{
Rows = imageNames;
CellSize = size;
}

    public List<string> Rows { get; private set; }

    public CGSize CellSize { get; set; }


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

    public override nint GetItemsCount(UICollectionView collectionView, nint section)
    {
        return Rows.Count;
    }

    public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
    {
        var cell = (CollectionCellClass)collectionView.DequeueReusableCell("CollectionCell", indexPath);

        cell.ChangeImage(Rows[indexPath.Row]);

        var frame = cell.Frame;
        frame.Size = CellSize;
        cell.Frame = frame;

        return cell;
    }

partial class CollectionCellClass : UICollectionViewCell
{
public CollectionCellClass (IntPtr handle) : base (handle)
{
}

            public void ChangeImage(string imageName)
        {
        CollectionImage.Image = UIImage.FromBundle("1.png");
    }
}

Best Answer

Answers

  • RobertPerlyRobertPerly USMember

    You might need to turn off AutoLayout for the control

    ControlName.translatesAutoresizingMaskIntoConstraints = YES;

    Then set the frame

  • Yasir.aliYasir.ali USMember
    edited August 2016

    thanks Robert for your answer ..
    i need to tell you that cell size get changed by changing the frame but space occupied on controller's main view to display cell remains the same as defined in collectionview's cell size and hence cell contents are going out of bound.
    i.e initially i defined cell size 320x190 on storyboard and later when i run project on 6s plus cell's frame size changed to 414x190 but scrollview's content size remain same as w.r.t 320x190 and it tries to fit new content into previous scrollview content size which is still according to cell size 320x190

  • XAMARINROCKXAMARINROCK Member ✭✭
    edited November 2018

    Hello there
    I am developing my first ios app, and i am wondering if i can find a small example on how, or where to override UICollectionViewDelegateFlowLayout.I am wondering what i need to create ??

    The normal scenario that i use is on below.

    DataTable subitemdt = getdata();
    UICollview.RegisterClassForCell(typeof(gvitemsearch_layout), gvitemsearch_layout.Cellid);
    gv.Source = new gvsourcesea(subitemdt, this, gv);

    internal class gvsourcesea : UICollectionViewSource
    {
    private DataTable subdt;
    private UIViewController vc;
    private UICollectionView gv;

    public gvsourcesea(DataTable subdt, UIViewController vc, UICollectionView gv)
    {
    this.subdt = subdt;
    this.vc = vc;
    this.gv = gv;
    }
    public override nint NumberOfSections(UICollectionView collectionView)
    {
    // We only have one section
    return 2;
    }

    public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
    {
    // Get a reusable cell and set {it's~>its} title from the item
    var cell = collectionView.DequeueReusableCell("sea_cel_id", indexPath) as gvitemsearch_layout;
    DataRow address = subdt.Rows[indexPath.Row];
    cell.updatecell(address);

    return cell;
    }
    public override nint GetItemsCount(UICollectionView collectionView, nint section)
    {
    // Return the number of items
    return subdt.Rows.Count;
    }
    }

    public partial class gvitemsearch_layout : UICollectionViewCell
    {
    public static NSString Cellid = new NSString("itemsea_id");
    public gvitemsearch_layout (IntPtr handle) : base (handle)
    {
    }
    internal void updatecell(DataRow address)
    {
    nam.Text = address["IName"].ToString();

    }
    }

Sign In or Register to comment.