Bug 30999 - UICollectionViewCell.ContentView is null in constructors in iOS 9
Summary: UICollectionViewCell.ContentView is null in constructors in iOS 9
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 8.10
Hardware: PC Mac OS
: Normal normal
Target Milestone: 9.0 (iOS9)
Assignee: Alex Soto [MSFT]
Depends on:
Reported: 2015-06-10 17:17 UTC by Frank A. Krueger
Modified: 2015-06-26 10:38 UTC (History)
5 users (show)

Is this bug a regression?: ---
Last known good build:

Objective C Test Case (36.42 KB, application/zip)
2015-06-11 02:36 UTC, Alex Soto [MSFT]
Xamarin.iOS Test Case (11.90 KB, application/zip)
2015-06-11 02:37 UTC, Alex Soto [MSFT]

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Frank A. Krueger 2015-06-10 17:17:18 UTC
Building with the iOS 8 SDK, running on iOS 9. So just a heads up

UICollectionViewCell.ContentView is now null in constructors.

For example, this code will now crash with an NRE:

	class NotReadyThumbnailCell : UICollectionViewCell
		public NotReadyThumbnailCell (IntPtr handle)
			: base (handle)
			var b = ContentView.Bounds;
Comment 1 Sebastien Pouliot 2015-06-10 21:02:41 UTC
That sounds like an issue (or change) in iOS9 itself, i.e. it's not related to the SDK/tools building it.

There's been other issues like this in the past (years) where the lifecycle of objects changed, including when things get initialized. Apple often does not fully initialize everything in `init` when there are events laters (it's a a common issue with views).

We'll confirm that asap with an ObjC sample doing the same.
Comment 2 Alex Soto [MSFT] 2015-06-11 02:36:24 UTC
Created attachment 11565 [details]
Objective C Test Case
Comment 3 Alex Soto [MSFT] 2015-06-11 02:37:47 UTC
Created attachment 11566 [details]
Xamarin.iOS Test Case
Comment 4 Alex Soto [MSFT] 2015-06-11 02:41:40 UTC
Hey Frank

I did some testing in ObjC land and it seem that I cannot reproduce the issue there but I think I found out why

From https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/#//apple_ref/occ/instm/UICollectionView/dequeueReusableCellWithReuseIdentifier:forIndexPath:

There is this note:

> If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithFrame: method. 

So we must use the initWithFrame: ctor. I am no longer able to reproduce the crash in iOS 9 if I change the code to

public class NotReadyThumbnailCell : UICollectionViewCell {		
	public NotReadyThumbnailCell (IntPtr handle) : base (handle)
		var b = ContentView.Bounds;
		Console.WriteLine (b);
		BackgroundColor = UIColor.Blue;

	[Export ("initWithFrame:")]
	public NotReadyThumbnailCell (CGRect frame) : base (frame)
		var b = ContentView.Bounds;
		Console.WriteLine (b);
		BackgroundColor = UIColor.Red;

Now that I am exporting to unmanaged land my custom managed initWithFrame: ctor, I always hit the initWithFrame: code path now instead of the IntPtr one and voila pure magic.

Hopes this solves your issue, I have attached both of my test cases (Xamarin.iOS and ObjC) to see if they match what you are doing. If this fixes your issue please let me know.


Comment 5 Frank A. Krueger 2015-06-11 12:00:20 UTC
Great to hear! Thank you for the research. I will switch all my code to the proper ctor.

Love these lifecycle changes! :-)
Comment 6 Alex Soto [MSFT] 2015-06-11 12:01:52 UTC
Great! Glad I could help!
Comment 7 Saurabh 2015-06-25 02:44:27 UTC
As per Comment#5 changing the status to Verified