Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

UICollectionView in a UITableViewCell from a Nib...

$
0
0

I have attached a .jpg that illustrates what I'm trying to achieve. The text version:

I've got a TableView, and within each TableView cell I have a label and a CollectionView. Both of the TableView cells and the CollectionView cells are (will be) populated from a nib file.

The TableViewSource create its cells with this code:

    public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
    {
        var cell = tableView.DequeueReusableCell (GroupCell2.Key, indexPath) as GroupCell2;

        cell.AssignGroup(_groups[indexPath.Row].Id);
        return cell;
    }    

The CollectionViewSource creates it cells with this code:

public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
    var cell = collectionView.DequeueReusableCell(PlantCollectionViewCell.Key, indexPath) as PlantCollectionViewCell;

    Plant plant = _plants[indexPath.Row];
    cell.AssignPlant(plant.Id);

    return cell;
}

However, the DequeueReusableCell() call causes an exception:

Instance {System.NullReferenceException: Object reference not set to an instance of an object at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject[NSObject] (IntPtr ptr, System.Type type, MissingCtorResolution missingCtorResolution) [0x00037] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:377 at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject (IntPtr ptr, IntPtr klass, MissingCtorResolution missingCtorResolution) [0x00013] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:360 at MonoTouch.ObjCRuntime.Runtime.GetNSObject (IntPtr ptr, MissingCtorResolution missingCtorResolution) [0x00021] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:442 at MonoTouch.ObjCRuntime.Runtime.TryGetOrConstructNSObjectWrapped (IntPtr ptr) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:673 at (wrapper native-to-managed) MonoTouch.ObjCRuntime.Runtime:TryGetOrConstructNSObjectWrapped (intptr) at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:IntPtr_objc_msgSend_IntPtr_IntPtr (intptr,intptr,intptr,intptr) at MonoTouch.UIKit.UICollectionView.DequeueReusableCell (MonoTouch.Foundation.NSString reuseIdentifier, MonoTouch.Foundation.NSIndexPath indexPath) [0x00038] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UICollectionView.g.cs:251 at CPOrders.iOS.PlantCollectionViewSource.GetCell (MonoTouch.UIKit.UICollectionView collectionView, MonoTouch.Foundation.NSIndexPath indexPath) [0x00018] in CollectionViewController.cs:122 at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38 at CPOrders.iOS.Application.Main (System.String[] args) [0x00008] in Main.cs:16 } System.NullReferenceException

I've figured out one key difference is:

The TableView is created this way:

var DetailViewController = new GroupTableViewController(coll.Id);
AppDelegate.Self._navController.PushViewController(DetailViewController, true);

The CollectionView is created by the DequeueReusableCell call from the TableView's GetCell method (above). When the CollectionView is created by instantiating a cell from a nib file, no controller is created. And I can't place a CollectionViewController within the TableViewCell in Interface Builder (which makes a certain amount of sense).

So, finally, the question:

What do I need to do to make this work? Am I going about this the wrong way? Do I HAVE to build my GroupCell in code so that I can create the controller for the CollectionView?


Viewing all articles
Browse latest Browse all 204402

Trending Articles