I've used CollectionViews in my project in another area of my project successfully with no issues, but this one simply won't display the data in the cell no matter what I do. I can see the cell border, it is the correct width and height, and when I step through the code I see the data get assigned to the label in the cell. I see the correct number of cells for the list of strings that's in the source, just get nothing displayed. I cannot figure out why. Anyone see any problems with my code? This should be pretty simple, and I've been at it for 3 days now with no results.
My Cell Code:
public partial class PhoneNumberCell : UICollectionViewCell
{
public static readonly NSString Key = new NSString("PhoneNumberCell");
UILabel lblPhoneNumberInfo = new UILabel();
[Export("initWithFrame:")]
public PhoneNumberCell(CGRect frame) : base(frame)
{
// I set the frame and text color because in other instances
// of data not showing up, that's proved to be the issue.
// Apparently not here, I remove this code and still no text in the cell.
lblPhoneNumberInfo.Frame = new CGRect(0,0,ContentView.Frame.Width, 26f);
lblPhoneNumberInfo.TextColor = UIColor.Black;
// I do see the border around the cell though.
ContentView.Layer.BorderColor = UIColor.LightGray.CGColor;
ContentView.Layer.BorderWidth = 2.0f;
ContentView.BackgroundColor = UIColor.White;
ContentView.Transform = CGAffineTransform.MakeScale(0.8f, 0.8f);
}
public void SetData(string pPhoneNumberInfo)
{
lblPhoneNumberInfo.Text = pPhoneNumberInfo;
}
protected PhoneNumberCell(IntPtr handle) : base(handle)
{
// Note: this .ctor should not contain any initialization logic.
}
}
The XIB:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7026.1" />
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" />
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder" />
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" customClass="PhoneNumberCell" id="cZE-iV-UFb">
<rect key="frame" x="0.0" y="0.0" width="50" height="50" />
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES" />
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
<rect key="frame" x="0.0" y="0.0" width="50" height="50" />
<autoresizingMask key="autoresizingMask" />
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite" />
</view>
</collectionViewCell>
</objects>
</document>
The Source:
public class PhoneNumberSource : UICollectionViewSource
{
public IList<string> PhoneNumbers;
public override nint NumberOfSections(UICollectionView collectionView)
{
return 1;
}
public override nint GetItemsCount(UICollectionView collectionView, nint section)
{
return PhoneNumbers.Count;
}
public PhoneNumberSource(IList<string> pPhoneNumbers)
{
PhoneNumbers = pPhoneNumbers;
}
public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
var phoneNumberCell = (PhoneNumberCell)collectionView.DequeueReusableCell(PhoneNumberCell.Key, indexPath);
string phoneinfo = PhoneNumbers[indexPath.Row];
phoneNumberCell.SetData(phoneinfo);
return phoneNumberCell;
}
}
My CollectionView layout:
PhoneNumberList.CollectionViewLayout = new UICollectionViewFlowLayout()
{
ItemSize = new CGSize((float)UIScreen.MainScreen.Bounds.Size.Width, 26f),
HeaderReferenceSize = new CGSize(25, 25),
SectionInset = new UIEdgeInsets(0, 0, 0, 0),
ScrollDirection = UICollectionViewScrollDirection.Vertical,
MinimumInteritemSpacing = 0, // minimum spacing between cells
MinimumLineSpacing = 0,
};
The solution is that you need to AddSubview
for the ContentView
like below:
ContentView.AddSubview(lblPhoneNumberInfo);