1

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

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

我使用 linq to xml 从 2 个 xml 文件中获取数据。一切正常,数据加载良好。我需要知道的是根据视图 1 中所做的选择在数据源 2 中加载数据。为此,我需要将视图 1 中的 ID 传递给类(数据源)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() 方法,但无济于事。我是单声道触摸的新手。

4

1 回答 1

0

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

重用 View2 并没有错,只是需要更多的调试才能准确找出问题所在。

于 2012-06-13T17:51:17.547 回答