In my MonoDevelop project I have an iPhone app. I have two different views. Each view contains a UITable. For view 1 I have class 1 hooked to the UITableViewController as Datasource 1. The class is specified in Interface Builder on the UITableViewController class property.
For View 2 I have a class 2 hooked up as Datasource 2. Also hooked up in interface builder on the UITableViewController class property. Both classes (i.e. Datasources) feed the tables with data. View 2 also has a custom cell and because of this loads asynchronous.
I get the data from 2 xml files using linq to xml. Everything works and the data loads great. What I need to do know is to load data in Datasource 2 based on the selection made in View 1. To do this I need to pass an ID from view 1 to Class(Datasource) 2. This works great and the data for the selected ID loads. The problem is when i tap back on the Navigation controller and change my selection (ID) on the first screen, exactly the same data is shown which tells me the UITable does not refresh with the new selection.
Datasource Code that loads the rounds (ID):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Data;
namespace CurrieCup2012
{
[MonoTouch.Foundation.Register("FixturesTableViewController")]
public partial class MyTableViewController : UITableViewController
{
static NSString CellID = new NSString ("MyIdentifier");
List<Fixtures> Fixtures;
// Constructor invoked from the NIB loader
public MyTableViewController (IntPtr p) : base (p)
{
}
// The data source for our TableView
class DataSource : UITableViewDataSource
{
MyTableViewController tvc;
public DataSource (MyTableViewController tableViewController)
{
this.tvc = tableViewController;
}
public override int RowsInSection (UITableView tableView, int section)
{
return tvc.Fixtures.Count;
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell (CellID);
if (cell == null)
{
cell = new UITableViewCell (UITableViewCellStyle.Default, CellID);
}
// Customize the cell here...
Fixtures fixtures = tvc.Fixtures.ElementAt(indexPath.Row);
cell.TextLabel.Text = fixtures.RoundID;
cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
return cell;
}
}
// This class receives notifications that happen on the UITableView
class TableDelegate : UITableViewDelegate
{
MyTableViewController tvc;
public TableDelegate (MyTableViewController tableViewController)
{
this.tvc = tableViewController;
}
DetailFixturesViewer detailsViewController;
//SelectedRound round = new SelectedRound();
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
if (detailsViewController == null)
detailsViewController = new DetailFixturesViewer();
Fixtures fixtures = tvc.Fixtures.ElementAt(indexPath.Row);
//Console.WriteLine(fixtures.RoundID);
detailsViewController.CurrentFixtures = fixtures;
IntPtr vd;
MyTableViewController2 mtv = new MyTableViewController2(vd);
mtv.round = fixtures.RoundID;
// round.RoundID = tvc.Fixtures.ElementAt(indexPath.Row).RoundID;
// Console.WriteLine("Set Selected round on row selected: " + round.RoundID);
//SelectedRound selround = new SelectedRound();
//selround.RoundID = fixtures.RoundID;
SelectedRound.RoundID = fixtures.RoundID;
//Console.WriteLine(round.RoundID);
//Console.WriteLine(indexPath.Row.ToString());
//Console.WriteLine(newsArticle.Headline.ToString());
//Console.WriteLine(newsArticle.Link.ToString());
tvc.NavigationController.PushViewController(detailsViewController, true);
tvc.NavigationController.ViewWillAppear(true);
detailsViewController.CurrentFixtures.RoundID = fixtures.RoundID;
detailsViewController.CurrentFixtures.Date = fixtures.Date;
detailsViewController.Title = fixtures.RoundID;
}
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
//Console.WriteLine("Linq");
XDocument rss =
XDocument.Load("http://curriecup.dutch-vegas.com/XML/rounds.xml");
var query = from item in rss.Descendants("Round")
select new Fixtures
{
RoundID = "Round " + (string) item.Element("roundid"),
Date = (string) item.Element("date")
/*GameID = (string) item.Element("gameid"),
Team1 = (string) item.Element("team1"),
Team2 = (string) item.Element("team2"),
Team1Image = (string) item.Element("team1image"),
Team2Image = (string) item.Element("team2image"),
Date = (string) item.Element("date"),
Location = (string) item.Element("location")*/
};
Fixtures = query.Distinct().ToList();
TableView.Delegate = new TableDelegate (this);
TableView.DataSource = new DataSource (this);
Title = "Rounds";
}
}
}
Datasource that loads the detail data based on the round (ID) selection from datasource 1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Data;
using System.IO;
namespace CurrieCup2012
{
[MonoTouch.Foundation.Register("DetailFixturesTableViewController")]
public partial class MyTableViewController2 : UITableViewController
{
static NSString CellID2 = new NSString ("MyIdentifier2");
List<DetailFixtures> DetailFixtures;
public string round{ get; set; }
// Constructor invoked from the NIB loader
public MyTableViewController2 (IntPtr p) : base (p)
{
}
// The data source for our TableView
class DataSource : UITableViewDataSource
{
MyTableViewController2 tvc;
public DataSource (MyTableViewController2 tableViewController)
{
this.tvc = tableViewController;
}
public override int RowsInSection (UITableView tableView, int section)
{
return tvc.DetailFixtures.Count;
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
Dictionary<int,FixturesCell> _cellControllers = new Dictionary<int, FixturesCell>();
var cell2 = tableView.DequeueReusableCell (CellID2);
FixturesCell fixtureCell = null;
DetailFixtures detailfixtures = tvc.DetailFixtures.ElementAt(indexPath.Row);
if (cell2 == null)
{
//cell2 = new UITableViewCell (UITableViewCellStyle.Default, CellID2);
fixtureCell = new FixturesCell();
cell2 = fixtureCell.Cell;
cell2.Tag = Environment.TickCount;
_cellControllers[cell2.Tag] = fixtureCell;
}
else
{
fixtureCell = _cellControllers[cell2.Tag];
}
// Customize the cell here...
fixtureCell.Team1 = detailfixtures.Team1;
fixtureCell.Team2 = detailfixtures.Team2;
fixtureCell.Location = detailfixtures.Location;
fixtureCell.Date = detailfixtures.Date;
if(!string.IsNullOrEmpty(detailfixtures.Team1Image))
{
if(File.Exists("Images/"+detailfixtures.Team1Image))
{
fixtureCell.Team1Image = UIImage.FromFile("Images/"+detailfixtures.Team1Image);
}
}
if(!string.IsNullOrEmpty(detailfixtures.Team2Image))
{
if(File.Exists("Images/"+detailfixtures.Team2Image))
{
fixtureCell.Team2Image = UIImage.FromFile("Images/"+detailfixtures.Team2Image);
}
}
/*DetailFixtures detailfixtures = tvc.DetailFixtures.ElementAt(indexPath.Row);
cell2.TextLabel.Text = detailfixtures.Team1;
//cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton;*/
return cell2;
}
}
// This class receives notifications that happen on the UITableView
class TableDelegate : UITableViewDelegate
{
MyTableViewController2 tvc;
public TableDelegate (MyTableViewController2 tableViewController)
{
this.tvc = tableViewController;
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
/*if (detailsViewController == null)
detailsViewController = new DetailFixturesViewer();
DetailFixtures detailFoxtures = tvc.DetailFixtures.ElementAt(indexPath.Row);
detailsViewController.CurrentFixtures = fixtures;
//Console.WriteLine(indexPath.Row.ToString());
//Console.WriteLine(newsArticle.Headline.ToString());
//Console.WriteLine(newsArticle.Link.ToString());
tvc.NavigationController.PushViewController(detailsViewController, true);
tvc.NavigationController.ViewWillAppear(true);*/
}
}
public override void ViewDidLoad ()
{
//SelectedRound selround = new SelectedRound();
string selectedRound = SelectedRound.RoundID.Replace("Round ", "");
Console.WriteLine("read selected Round viewdidload table class: " + selectedRound);
base.ViewDidLoad ();
XDocument rss =
XDocument.Load("http://curriecup.dutch-vegas.com/XML/fixtures.xml");
var query = from item in rss.Descendants("Game") where (string) item.Element("roundid") == selectedRound
select new DetailFixtures
{
//RoundID = "Round " + (string) item.Element("roundid")
GameID = (string) item.Element("gameid"),
Team1 = (string) item.Element("team1"),
Team2 = (string) item.Element("team2"),
Team1Image = (string) item.Element("team1image"),
Team2Image = (string) item.Element("team2image"),
Date = (string) item.Element("date"),
Location = (string) item.Element("location")
};
//Console.WriteLine("test");
DetailFixtures = query.Distinct().ToList();
TableView.Delegate = new TableDelegate (this);
TableView.DataSource = new DataSource (this);
//Title = "Fixtures";
Console.WriteLine("Reload");
}
}
}
I have tried calling the ReloadData() method on the table but to no avail. I am new new to mono touch.
The simplest thing to try is to create a new instance of View2 every time, instead of trying to re-use an existing instance.
There is nothing wrong with re-using View2, it just would require some more debugging to find out exactly where the problem is.