So I am encountering some issues with a UICollectionView I have in my Xamarin iOS project.
The cells for the CollectionView are not expanding with the label in them. The label in the cell has lines set to 0 and the following constraints:
(top, bottom, leading, trailing) to superview
Height >= 10
In my ViewDidLoad() I setup the CollectionView as follows:
var source = new CustomCollectionViewSource(collectionView);
collectionView.Source = source;
collectionView.SetCollectionViewLayout(new UICollectionViewFlowLayout()
{
ScrollDirection = UICollectionViewScrollDirection.Vertical,
EstimatedItemSize = new CGSize(collectionView.Frame.Width, 10)
}, false);
var set = this.CreateBindingSet<MainViewController, MainViewModel>();
set.Bind(source).For(v => v.ItemsSource).To(vm => vm.DataSource);
set.Apply();
and my CollectionViewSource
public class CustomCollectionViewSource : MvxCollectionViewSource
{
public IList<Item> Messages { get; set; }
public override void ReloadData()
{
var list = (ItemsSource as IEnumerable<Item>).ToArray();
Messages = list;
}
public CustomCollectionViewSource(UICollectionView collectionView) : base(collectionView)
{
collectionView.RegisterNibForCell(CustomCollectionViewCell.Nib, CustomCollectionViewCell.Key);
}
public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
var cell = collectionView.DequeueReusableCell(CustomCollectionViewCell.Key, indexPath) as CustomCollectionViewCell;
cell.DataContext = Messages.ElementAtOrDefault(indexPath.Row);
return cell;
}
}
I am trying to have the cells be the width of the collection view and for the height to be flexible; however, whenever I populate the cells, I always get something like this:
The cells are cutoff and have an ellipses at the end. I've read online that the constraints must be right to dynamically resize, so is there something I'm missing?
I also read that to get a calculated height for each cell I can load the nib of the cell, populate it, then set the size manually in a custom UICollectionViewDelegate. Cool idea, but I could find any working examples for Xamarin iOS.
Please let me know if there is something I'm missing or any other info you require. Thanks in advance!
1.Make your label support multiple lines by
textLabel.LineBreakMode = UILineBreakMode.WordWrap;
textLabel.Lines = 0;
2.Set the cell estimated height
cell.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
Things to note:
To define the cell’s height, you need an unbroken chain of
constraints and views (with defined heights) to fill the area
between the content view’s top edge and its bottom edge.
If your views have intrinsic content heights, the system uses those values. If not, you must add the appropriate height constraints, either to the views or to the content view itself.