刷新类绑定表数据monotouch

时间:2012-06-09 13:12:26

标签: c# iphone uitableview xamarin.ios monodevelop

在我的MonoDevelop项目中,我有一个iPhone应用程序。我有两种不同的看法。每个视图都包含一个UITable。对于视图1,我将类1挂钩到UITableViewController作为Datasource 1.该类在Interface Builder上的UITableViewController类属性中指定。

对于View 2,我有一个连接为Datasource 2的类2.还连接到UITableViewController类属性的接口构建器中。两个类(即数据源)都为表提供数据。 View 2也有一个自定义单元格,因此加载异步。

我使用linq到xml从2个xml文件中获取数据。一切正常,数据加载很好。我需要知道的是根据在View 1中做出的选择在Datasource 2中加载数据。为此,我需要将ID从视图1传递给Class(Datasource)2。这很好用,并且所选的数据ID加载。问题是当我重新点击导航控制器并在第一个屏幕上更改我的选择(ID)时,显示的数据完全相同,告诉我UITable不会刷新新选择。

加载轮次的数据源代码(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";

        }
    }
}

根据数据源1中的回合(ID)选择加载详细数据的数据源:

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");
        }
    }
}

我试过在表上调用ReloadData()方法,但无济于事。我是单声道触摸的新手,对此的任何帮助将不胜感激。

由于 Ĵ

1 个答案:

答案 0 :(得分:0)

最简单的方法是每次都创建一个View2的新实例,而不是尝试重用现有的实例。

重新使用View2没有任何问题,只需要更多的调试就可以找到问题的确切位置。