Access ViewModel from Tablesource - c#

My code looks similiar to this https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/Sample%20-%20CirriousConference/Cirrious.Conference.UI.Touch/Views/TwitterView.cs
In the TableSource class, is it possible to access the TwitterViewModel ViewModel Property without creating a new instance of the vm, making it static or using event-aggregation?
for example:
public override float GetHeightForRow (UITableView tableView, NSIndexPath indexPath)
{
return ViewModel.DoGetHeightForRow();
}

Since that class is nested within the View, you can just add access - exactly as you would any other C# class - e.g:
public class TableSource : MvxSimpleTableViewSource
{
private TwitterView _parent;
public TableSource (UITableView tableView, TwitterView parent)
: base(tableView, TweetCell3.Identifier, TweetCell3.Identifier)
{
_parent = parent;
tableView.RegisterNibForCellReuse(UINib.FromName(TweetCell3.Identifier, NSBundle.MainBundle), TweetCell3.Identifier);
}
public override float GetHeightForRow (UITableView tableView, NSIndexPath indexPath)
{
return _parent.SomeMethod(indexPath);
}
}

Related

how to open UIViewController when i click RowSelected from UITableViewSource

i want open viewcontroller when i click RowSelected from UITableViewSource
i use this code when i open UIViewController
Any help would be greatly appreciated.
thanks
MainStudUn controller =this.Storyboard.InstantiateViewController("MainStudUn") as MainStudUn;
this.NavigationController.PushViewController(controller, true);
but i have issue when used this code from UITableViewSource
class EmployeesTVS : UITableViewSource
{
List<Employee> employees;
public EmployeesTVS(List<Employee> employees)
{
this.employees = employees;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var cell = (EmployeeCell) tableView.DequeueReusableCell("Cell_id", indexPath);
var employee = employees[indexPath.Row];
cell.updatecell(employee);
return cell;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return employees.Count;
}
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
var SelectName = employees[indexPath.Row];
Globals.AskX = SelectName.Fullname;
Globals.AnswerX = SelectName.Department;
//OpnWerd();
//MainStudUn controller = this.Storyboard.InstantiateViewController("MainStudUn") as MainStudUn;
//this.NavigationController.PushViewController(controller, true);
}
}
Set the ViewController itself as parameter in the initializing constructor of the dataSource.
in ViewController
this.TableView.Source = new EmployeesTVS(tableItems, this);
in dataSource
AskStud owner; //this is your VC class not ViewController,it's just a sample.
public EmployeesTVS(List<Employee> employees, AskStud owner)
{
this.employees = employees;
this.owner = owner;
}
Usage:
MainStudUn controller =this.owner.Storyboard.InstantiateViewController("MainStudUn") as MainStudUn;
//OR
//UIStoryboard Storyboard = UIStoryboard.FromName("StoryBoardName", null);
//MainStudUn controller = Storyboard.InstantiateViewController("MainStudUn") as MainStudUn;
this.owner.NavigationController.PushViewController(controller, true);

How to break reference cycle between view controller and data source

Consider this simple example:
public partial class TableViewController : UITableViewController
{
public TableViewController (IntPtr handle) : base (handle)
{
}
protected override void Dispose (bool disposing)
{
Console.WriteLine (String.Format ("{0} controller disposed - {1}", this.GetType (), this.GetHashCode ()));
base.Dispose (disposing);
}
public override void ViewDidLoad ()
{
//TableView.Source = new TableSource(this);
TableView.Source = new TableSource();
}
}
public class TableSource : UITableViewSource {
private TableViewController controller;
string CellIdentifier = "TableCell";
public TableSource ()
{
}
public TableSource (TableViewController controller)
{
this.controller = controller;
}
public override nint RowsInSection (UITableView tableview, nint section)
{
return 1;
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell (CellIdentifier);
//if there are no cells to reuse, create a new one
if (cell == null){
cell = new UITableViewCell (UITableViewCellStyle.Default, CellIdentifier);
}
cell.TextLabel.Text = "test";
return cell;
}
}
I've noticed that the view controller (TableViewController) is never released. The table view controller has a reference to the data source, but the data source also has a reference to the table view controller.
With TableView.Source = new TableSource(); the view controller gets released, with TableView.Source = new TableSource(this); it's not.
How should this reference cycle be broken so that everything get released?
Edit:
Now I tried the WeakReference:
Through using a WeakReference the Dispose method is called, when the view controller is popped off the navigation stack.
In ViewDidLoad:
TableView.Source = new TableSource(new WeakReference<TableViewController> (this));
In the datasource:
private WeakReference<TableViewController> controller;
public TableSource (WeakReference<TableViewController> controller)
{
this.controller = controller;
}
I built this into my real project, but how can I access my controller? I get the message
Type 'System.WeakReference' does not contain a definition for 'xxx' and no extension method 'xxx' of type 'System.WeakReference' could be found. Are you missing an assembly reference?
You work with Xamarin, as I see? Have you tried WeakReference?
https://msdn.microsoft.com/en-us/library/system.weakreference(v=vs.110).aspx
PS:
private WeakReference weakController;
to set:
this.weakController = new WeakReference(controller);
to get:
if (weakController.isAlive)
{
TableViewController controller = weakController.Target as TableViewController;
}
change
public partial class TableViewController : UITableViewController
to
public partial class TableViewController : UITableViewController, UITableViewSource
and in ViewDidLoad just do
self.TableView.Source = self;
the source property is a weak reference internally already so your have no problem of managing that. It is a convenience property to make the tbaleviewcontroller the delegate and datasource altogether. (Just like you would in native iOS)
You can move the methods into the controller itself which would be less of a hassle than WeakReference. Then mark them with export attribute which then allows you to set the UITableView.WeakDataSource property to the controller itself.
[Export("tableView:numberOfRowsInSection:")]
public nint RowsInSection (UITableView tableview, nint section)
[Export("tableView:cellForRowAtIndexPath:")]
public UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
Once they are moved you can attach the datasource:
public override void ViewDidLoad ()
{
TableView.WeakDataSource = this;
}

How to access NavigationController.PushViewController under UITableViewSource?

How can I access NavigationController inside UITableviewSource class?
On Row selection I want to navigate to another UIController.
This is my code,
public class RootTableSource : UITableViewSource
{
IList<VendorDetails> tableItems;
string cellIdentifier = "UIViewController";
ReportsList reportList;
AddNewReport addnewReport;
public RootTableSource()
{
}
public RootTableSource (IEnumerable<VendorDetails> items)
{
tableItems = items.ToList ();
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
tableView.DeselectRow (indexPath, true);
// Redirect to another UIController....
}
public VendorDetails GetItem (int id)
{
return tableItems [id];
}
}
have you instance UINavigationController in AppDelegate ?
like this...
#property (strong, nonatomic) UINavigationController *navigationViewController;
then you should can access UINavigationController from AppDelegate delegate
#import "AppDelegate.h"
#implementation yourClassName
-(void)functionName{
AppDelegate *appdelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
appdelegate.navigationViewController; //your UINavigationController
[appdelegate.navigationViewController pushViewController:yourUIViewController animated:YES];
}
#end
wish help ~
edit:
Sorry i have never use Xamarin before
so i think this is a bad way to implement ...
but it look work
AppDelegates.cs
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
UIWindow window;
HomeScreen home;
public static UINavigationController navigation;
//set navigation public and static
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window = new UIWindow (UIScreen.MainScreen.Bounds);
home = new HomeScreen ();
navigation = new UINavigationController(home);
window.RootViewController = navigation;
window.MakeKeyAndVisible ();
return true;
}
}
yourClassName.cs
AppDelegate.navigation
//access navigation
wish help again ...
The way I do this is by passing a reference to the TableViewController when I create my Source. Then the Source can use this reference to access it's parent's NavigationController.
UITableViewController _parent;
public RootTableSource (IEnumerable<VendorDetails> items, UITableViewController parent)
{
tableItems = items.ToList ();
_parent = parent;
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
tableView.DeselectRow (indexPath, true);
_parent.NavigationController.PushViewController(...);
}

How to access main UIViewController from UITableView RowSelect Event Monotouch?

I have a UITAbleView on my main ViewController. Tableview is subclasses as seen below. When a user selects a row I want to switch to a new view by calling a routine on the main ViewController. However, i'm not able to access my main viewcontroller from the subclass. How should I go about this?
public class TableSource : UITableViewSource
{
string[] tableItems;
string cellIdentifier = "TableCell";
public TableSource(string[] items)
{
tableItems = items;
}
public override int RowsInSection(UITableView tableview, int section)
{
return tableItems.Length;
}
public override UITableViewCell GetCell(UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell(cellIdentifier);
if (cell == null)
cell = new UITableViewCell(UITableViewCellStyle.Default, cellIdentifier);
cell.TextLabel.Text = tableItems[indexPath.Row];
return cell;
}
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
new UIAlertView("Row Selected", tableItems[indexPath.Row], null, "OK", null).Show();
tableView.DeselectRow(indexPath, true);
//Call routine in the main view controller to switch to a new view
}
}
I happened to come across this post after finding and commenting on a similar post:
Xamarin UITableView RowSelection
While passing an instance of your UIViewController to your TableSource is one way to solve this problem, it does have its drawbacks. Mainly that you've tightly coupled your TableSource to that particular type of UIViewController.
What I would suggest instead is to create an EventHandler in your UITableViewSource like so:
public event EventHandler ItemSelected;
I would also set up a getter property for the selected item:
private selectedItem
public MyObjectType SelectedItem{
get{
return selectedItem;
}
}
I would then update the RowSelected method like so:
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
selectedItem = tableItems[indexPath.Row];
ItemSelected(this, EventArgs.Empty);
}
Your UIViewController could then listen to the ItemSelected event and do whatever it needs. This allows you to reuse your UITableViewSource for multiple Views and ViewControllers.
Add it to your .ctor, e.g.
public TableSource(string[] items)
becomes:
public TableSource(string[] items, UIViewController ctl)
then keep a reference to it:
public TableSource(string[] items, UIViewController ctl)
{
tableItems = items;
controller = ctl;
}
and use it in your RowSelected call:
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
new UIAlertView("Row Selected", tableItems[indexPath.Row], null, "OK", null).Show();
tableView.DeselectRow(indexPath, true);
controller.DoWhatYouNeedWithIt ();
}
NSNotifications might be another option.
So in the RowSelected you post a notification:
NSNotificationCenter.DefaultCenter.PostNotificationName("RowSelected", indexPath);
And in the view controller you add an observer for the notification:
public override void ViewDidLoad()
{
base.ViewDidLoad();
NSNotificationCenter.DefaultCenter.AddObserver(new NSString("RowSelected"), RowSelected);
}
void RowSelected(NSNotification notification)
{
var indexPath = notification.Object as NSIndexPath;
// Do Something
}

How to navigate to a view controller when a specific row is selected in tableview?

I have a navigation controller. Inside it i have a view controller that has a button. When the button is clicked a popover shows with a tableview of items.
When an item is selected i want to either navigate to a different view controller or open up a view controller on top of the current one.
I hope i've made my objectives clear.
Below is my current code:
private UITableView tableView;
private List list;
private class TableViewDelegate : UITableViewDelegate
{
private List<string> list;
public TableViewDelegate(List<string> list)
{
this.list = list;
}
public override void RowSelected (
UITableView tableView, NSIndexPath indexPath)
{
//THIS IS WHERE I AM CURRENTLY PUTTING THE NAVIGATION CONTROLLER PUSHVIEWCONTROLLER. BUT ITS NOT WORKING !!!
}
}
private class TableViewDataSource : UITableViewDataSource
{
static NSString kCellIdentifier =
new NSString ("MyIdentifier");
private List<string> list;
public TableViewDataSource (List<string> list)
{
this.list = list;
}
public override int RowsInSection (
UITableView tableview, int section)
{
return list.Count;
}
public override UITableViewCell GetCell (
UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell =
tableView.DequeueReusableCell (
kCellIdentifier);
if (cell == null)
{
cell = new UITableViewCell (
UITableViewCellStyle.Default,
kCellIdentifier);
}
cell.TextLabel.AdjustsFontSizeToFitWidth = true;
cell.TextLabel.Font = UIFont.FromName("Helvetica", 14.0f);
cell.TextLabel.Text = list[indexPath.Row];
return cell;
}
}
in monotouch you should use
class TableDelegate : UITableViewDelegate
{
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
this.YourNavigationController.PushViewController(yourViewController,true);
}
}
also i recommend you to use monotouch.dialog
in your table source declare an event like this:
public class TableSourceAnimales : UITableViewSource
{
public event RowSelectedEventHandler RowSelectedEvent;
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
if (this.RowSelectedEvent != null) {
RowSelectedEvent (indexPath);
}
}
}
and in your uiviewcontroller like this
namespace yournamespace
public delegate void RowSelectedEventHandler(MonoTouch.Foundation.NSIndexPath indexpath);
public partial class yourclass : UIViewController
{
public override void ViewDidLoad ()
{
TableViewDataSource tablelist = new TableViewDataSource (yourlist);
table.Source = tablelist;
table.ReloadData ();
table.RowSelectedEvent += tablelist_RowSelectedEvent;
}
public void tablelist_RowSelectedEvent (NSIndexPath indexpath)
{
this.YourNavigationController.PushViewController(yourViewController,true);
//or what you want to do
}
}
Check which row is clicked in this function:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
And then show your view controller.
This will do the trick:
- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
YourViewController *vc = [[[YourViewController alloc] initWithNibName:#"YourViewController" bundle:[NSBundle mainBundle]] autorelease];
[self.navigationController pushViewController:vc animated:YES];
}
Something like this in mono:
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
var DetailViewController = new DetailViewController ();
// Pass the selected object to the new view controller.
controller.NavigationController.PushViewController(DetailViewController, true);
}
actually its like in Obj-C only the syntax is a bit different. Try to do it yourself next time, its easy.

Categories