Why isn't my custom UICollectionViewController's GetCell method called - xamarin.ios

I have a storyboard set up like so:
As you can see, I have a home screen. It has access to a navigation controller. It has two ContainerViews, one used as a sidepane, the other as the main content. I plan to swap things into this main content container as I need them.
One of the things I would like to show in this container, when it is selected from the sidepane, is a CollectionView of cells that show people. Each cell has a photo and the person's details.
The best code example I could find for CollectionView was the Xamarin StateRestoration sample project. It includes a storyboard. I have followed the recipe as best I can while working around my own specific storyboard.
The problem now is that the GetCell(UICollectionView collectionView, NSIndexPath indexPath) method used to populate each cell in the CollectionView is not being called. As far as I know I'm supposed to instantiate the CollectionViewController's Datasource property. I've tried this in AppDelegate and in the ViewDidLoad of my CollectionViewController itself, and GetCell is still not getting called. Why is this?
Any help is appreciated! Ask me to edit if you need more information.

You need to set both datasource and the delegate. As you've mentioned you've set the datasource. To set the delegate open up the storyboard in xcode, select UICollectionView and control drag to the its parent view controller. Have a look at this gif.
Also make sure you implement IUICollectionViewSource interface in your target view controller
public partial class DetailViewController : UIViewController, IUICollectionViewSource
{
protected DetailViewController(IntPtr handle) : base(handle)
{
// Note: this .ctor should not contain any initialization logic.
}
public nint GetItemsCount(UICollectionView collectionView, nint section)
{
return 10;
}
public UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
var cell = collectionView.DequeueReusableCell("PersonCell", indexPath) as UICollectionViewCell;
return cell;
}
}
EDIT 1:
You can also do this programmatically. Enter the storyboard, in Widget properties set the name for the UICollectionView - PersonCollection. Then in code behind view controller override ViewDidLoad and set WeakDelegate and WeakDataSource (weak because we definitely not want to create cyclic reference and memory leaks for iOS)
public override void ViewDidLoad()
{
base.ViewDidLoad();
PersonCollection.WeakDataSource = this;
PersonCollection.WeakDelegate = this;
}
Hope this helps!

Related

Xamarin iOS monotouch dialog - labels overlapping text input

When you create a dialog similar to the one below
And then navigate to a new view controller by tapping on a radiobutton group (in my example industry type), then return back your labels are endinf up overlapping the text. Tested on simulator and Apple iPad mini 2.
Has anyone found a way to fix this without creating a custom class ?
The only way was to create a new class and explicitly specify constraints by overriding GetCell method:
public class BaseEntryElement:EntryElement
{
.....
public override UITableViewCell GetCell (UITableView tv)
{
var c= base.GetCell (tv);
c.ContentView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints ();
c.ContentView.AddConstraints (
c.ContentView.Subviews[0].WithSameCenterY(c.ContentView),
c.ContentView.Subviews[0].AtLeftOf(c.ContentView,BaseEntryElement.offset),
c.ContentView.Subviews[0].AtTopOf(c.ContentView),
c.ContentView.Subviews[1].ToRightOf(c.ContentView.Subviews[0],40),
c.ContentView.Subviews[1].WithSameCenterY(c.ContentView.Subviews[0])
);
return c;
}
}

UICollectionView Help What is the Best Way for adding section in MVVMCross Xamarin?

How to add Section headers in UICollectionView in Xamarin.iOS and MVVMCross.
I just want to ask the best approach. I am searching from 2 days but unable to find any simple approach. My data is List and and Section Contain List
My design looks like
To add supplementary views within a UICollectionView, you'll need to override the CollectionViewSource to provide them.
The base code for this in MvvmCross is in https://github.com/MvvmCross/MvvmCross/blob/3.5/Cirrious/Cirrious.MvvmCross.Binding.Touch/Views/MvxCollectionViewSource.cs and https://github.com/MvvmCross/MvvmCross/blob/3.5/Cirrious/Cirrious.MvvmCross.Binding.Touch/Views/MvxBaseCollectionViewSource.cs
A good Xamarin tutorial for CollectionViews is http://developer.xamarin.com/guides/ios/user_interface/introduction_to_collection_views/
If you want to make the supplementary view bindable then you can do these by adapting the supplementary views in a similar way to the cells - e.g. copying and pasting the BindingContext`DataContext` code from https://github.com/MvvmCross/MvvmCross/blob/3.5/Cirrious/Cirrious.MvvmCross.Binding.Touch/Views/MvxCollectionViewCell.cs and then ensuring you set the DataContext when the supplementary cell is used or reused within the collection view source.
There is an open issue requesting this functionality within MvvmCross or a sample - https://github.com/MvvmCross/MvvmCross/issues/339 - but no-one (except me!) has ever commented on it... would be happy to see it added.
A bit late for the party, but here you have my working code for:
Using several sections in a collection
Use a header for each section
Change the data at your convenience:
public class SearchCollectionViewSource : MvxCollectionViewSource
{
private List<SearchResult> results { get { return ItemsSource as List<SearchResult>; } }
public SearchCollectionViewSource (UICollectionView collectionView) : base(collectionView) { }
public SearchCollectionViewSource (UICollectionView collectionView, NSString defaultCellIdentifier) : base(collectionView, defaultCellIdentifier) { }
public override UICollectionReusableView GetViewForSupplementaryElement (UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
{
return (HeaderView)collectionView.DequeueReusableSupplementaryView(elementKind, HeaderView.Key, indexPath);
}
public override nint NumberOfSections (UICollectionView collectionView)
{
return results.Count;
}
public override nint GetItemsCount (UICollectionView collectionView, nint section)
{
return results[(int)section].photos.Count;
}
protected override object GetItemAt(NSIndexPath indexPath)
{
return results [indexPath.Section].photos [indexPath.Row];
}
}
public sealed class HeaderView : UICollectionReusableView
{
public static string Key = "HeaderId";
[Export("initWithFrame:")]
public HeaderView(System.Drawing.RectangleF frame)
: base(frame)
{
UIView separator = new UIView() { Frame = new System.Drawing.RectangleF(0, 0, (float)UIScreen.MainScreen.Bounds.Width, 10), BackgroundColor = UIColor.LightGray };
AddSubview(separator);
}
}

MonoTouch.Dialog: How to access parent DialogViewController from within Element? Difference between Tapped event and Selected() method?

I'm subclassing a StringElement in MonoTouch.Dialog.
In there I can attach to the Tapped event or I can override Selected().
Both fire if I tap the element.
However, Selected() is giving me access to the DialogViewController the element is a member of, where this information is not passed to the Tapped event.
What is the logic here? Is an element supposed to know its DialogViewController or not? If yes: how to get tho the controller from the Tapped event then?
Found out myself by looking at the source on Github.
The only place where Tapped event is triggered, is from Selected(). So I think Tapped should really by of type EventHandler instead of Action.
public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath indexPath)
{
if (Tapped != null)
Tapped ();
tableView.DeselectRow (indexPath, true);
}
At the time I wrote that code, the idea was simply that with lambdas, you can pass whatever state you need to your Tapped handler, without using the object/EventArgs pattern.
So you would do something like:
var dialogViewController = CreateDvC ();
new StringElement ("....", () => {
// reference any variables here
// my container is:
Console.Writeline (dialogViewController);
}

Missing Reference to Navigation Controller in Monotouch

I am new to Monotouch and attempting to understand how some of the basics hang together. Hopefully someone out there will be able to assist.
I've created a test project in MonoDevelop based on the Multi-Screened Apps tutorial on the Xamarin site and have extended it to include a tableView. I am having issues with referencing the Navigation Controller in a view that I need to push a detail view onto to display the detail of an item tapped in the table via an accessory button. I know some of the coding is scrappy, just been trying to get it working at this stage rather than the clarity in the code! (I'm using the latest versions of all Mono tools/libraries etc and XCode 4 on Lion). Starting at the beginning here's the code in FinishedLaunching in AppDelegate.
window = new UIWindow (UIScreen.MainScreen.Bounds);
this.rootNavigationController = new UINavigationController();
// Create a new homescreen
HomeScreen homeScreen = new HomeScreen();
// Add the homescreen to the root navigation controller
this.rootNavigationController.PushViewController(homeScreen, false);
// Set the root view controller on the window - the navigation controller
// will handle the rest.
this.window.RootViewController = this.rootNavigationController;
// make the window visible
this.window.MakeKeyAndVisible ();
homeScreen just contains a button which loads a new view containing a table view (OrderList). Here's the button event handler.
void HandleOrdersButtonhandleTouchUpInside (object sender, EventArgs e)
{
if (orderListScreen == null)
orderListScreen = new OrderList();
NavigationController.PushViewController(orderListScreen, true);
}
This all works fine. I've got some dummy data that loads into the table view, which also works fine. OrderData is a simple class for testing which just contains a couple of properties. I've added an AccessoryButton to the cells and am trying to load a detail view when this is tapped. Here's the code that does this - comment in code where issue is! (I'd previously tested the AccessoryButtonTapped functionilty was working by just displaying an alert).
public override void AccessoryButtonTapped (UITableView tableView, NSIndexPath indexPath)
{
var dataSource = (OrdersTableViewDataSource)tableView.DataSource;
if (detailScreen == null)
detailScreen = new OrderDetailScreen();
OrderData theOrder = dataSource.OrdersData[indexPath.Row];
detailScreen.currentOrder = theOrder;
// Cant get a reference to NavigationController here to push the detail view!
// this.NavigationController is not available
this.NavigationController.PushViewController(detailScreen, true);
}
My understanding of NavigationControllers from what I've read so far is that this reference should be available through all views that originate from the root ViewController/NavigationController without the need to pass the reference from AppDelegate through the various view constructors?
Can anyone tell me what I might be missing here?
Thanks in advance.
** An update after reviewing Jason's comment: (Please let me know if this is the incorrect way to post updates)
So, I tried the following:
I saved a reference to the NavigationController in the constructor for the ViewController that contains the table view as follows:
public partial class OrderList : UIViewController
{
UINavigationController navController;
public OrderList () : base ("OrderList", null)
{
this.Title = "Orders";
navController = this.NavigationController;
}
Then passed that into the TableViewDelegate, where the AccessoryButtonTapped is handled, in the ViewDidLoad method.
public override void ViewDidLoad ()
{
orderTableView.DataSource = new OrdersTableViewDataSource();
orderTableView.Delegate = new OrdersTableViewDelegate(navController);
base.ViewDidLoad ();
}
Then referenced that in the TableViewDelegate:
public class OrdersTableViewDelegate : UITableViewDelegate
{
UINavigationController navController;
public OrdersTableViewDelegate(UINavigationController controller)
{
navController = controller;
}
// Rest of class definition
}
Then the reference to the NavigationController using navController compiles with the code as previously described using the following in the AccessoryButtonTapped method:
navController.PushViewController(detailScreen, true);
When I run this and tap on the AccessoryButton I get a null reference exception on navController. The reference to this.NavigationController in the ViewController constructor is null. Am I doing something in the wrong place or sequence?
Cheers
The NavigationController property is on your table's view controller. If you are trying to reference it from your table's datasource, you need to pass a reference to the controller when you create the datasource.

Have UITextField show keyboard (becomeFirstResponder) when UIView is shown

I have a UIViewController with a UITableView. The first row of the UITableView is a cell that has a UITextField in it. I'd like to improve my UI a little by showing the keyboard when the view is shown. I have tried putting the BecomeFirstResponder method in various events but have yet to get this to work.
Can someone please provide tips on how to present the keyboard when the view is presented via the PushViewController?
Thank you.
In your subclass of UITableViewSource or UITableViewDelegate try overriding WillDisplay method, like this:
public override void WillDisplay (UITableView tableView, UITableViewCell cell, NSIndexPath indexPath)
{
if(indexPath.Row == theRowIndexOfTheCellWithYourTextField){
yourTextField.BecomeFirstResponder();
}
}
It should work (note that you probably wish to add some code to make sure this is executed only once)
First of all, always use a UITableViewController derived class instead of a UIViewController when working with a UITableView. This will help you to resize the view, and makes sure fields are visible when the keyboard is shown.
You can show the keyboard for the first field by calling BecomeFirstResponder in the ViewDidAppear event. Example:
public class YourTableViewController : UITableViewController
{
private UITableView _yourTableView;
YourUITableViewSourceDerivedClass _yourSource;
public override void ViewDidLoad()
{
_yourTableView = new UITableView(View.Bounds, UITableViewStyle.Plain);
_yourTableView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight;
_yourSource = new YourUITableViewSourceDerivedClass();
_yourTableView.Source = _yourSource;
TableView = _yourTableView;
}
public override void ViewDidAppear(bool animated)
{
// Well, of course you call a method in your source to do this, but this is the idea:
_yourSource.textFieldOnFirstRow.BecomeFirstResponder();
}
}
in your viewdidload, put this:
[yourTextField becomeFirstResponder];
I am not sure at all is this works :S
please tell if it does/don't :)
You can try setting up a notification for when your cell view is loaded or appeared. Then you can call the becomeFirstResponder on the field after the notification is fired off.

Resources