选择Row for Details Xamarin IOS

时间:2016-12-12 17:13:04

标签: ios uitableview xamarin uiviewcontroller xamarin.ios

我有3个UITableViewControllers,它们都有数据。然后我有一个UIViewController。 我在其他控制器中也有一个UINavigationController用于导航。

  1. UITableViewController - >表
  2. 的UITableViewController - >表
  3. UITableViewController - 表格(从中选择行重复我的 详情页。)
  4. UIViewController - >详细信息页。
  5. 我的问题: My Row有按钮可以从1个控制器导航到其他控制器。 当我到达我的上一个UITableViewController以通过单击我的行中的Detail Button来打开UIViewController时,我的UIViewController会自动开始显示随机记录详细信息,然后自动停止在我从之前的UITableViewController中选择的记录。

    这对我来说很奇怪。我需要帮助来解决这个问题。 我正在研究Xamarin IOS项目。 感谢。

    第一个UITableViewController转到其他UIViewController,它在按钮上重复按钮执行按钮单击一行。

    using Foundation;
    using System;
    using UIKit;
    using System.Collections.Generic;
    using System.Linq;
    using CoreGraphics;
    using System.IO;
    using System.Xml.Serialization;
    
    namespace NaCLists
    {
        public partial class TblController : UITableViewController
        {
            private Guid _Id;
            private int _selectedFRowId;
            private int _selectedARowId;
            private int _selectedGRowId;
            private int _selectedCRowId;
    
            public TblController(IntPtr handle) : base(handle)
            {
            }
            public TblController()
            {
            }
    
            public override void ViewDidLoad()
            {
                base.ViewDidLoad();
                tblClists.Source = new TblSource(this, AuditorId, SelectedFacilityRowId, SelectedAssessmentRowId, SelectedGroupRowId);
            }
    
            public Guid AId
            {
                get
                {
                    return this._Id;
                }
                set
                {
                    this._Id = value;
                }
            }
    
            public int FRowId
            {
                get
                {
                    return this._FRowId;
                }
                set
                {
                    this._FRowId = value;
                }
            }
    
            public int ARowId
            {
                get
                {
                    return this._ARowId;
                }
                set
                {
                    this._ARowId = value;
                }
            }
            public int GRowId
            {
                get
                {
                    return this._GRowId;
                }
                set
                {
                    this._GRowId = value;
                }
            }
        }
    
        class TblSource : UITableViewSource
        {
            private List<Clists> _clists;
            NSString _cellID = new NSString("TableCell");
            TblController _parent;
            int _selectedF, _selectedA, _selectedG;
            Guid _Id;
    
            public TblSource(TblController parent, Guid AId, int FRowId, int ARowId, int GRowId)
            {
                _Id = AId;
                _selectedF = FRowId;
                _selectedA = ARowId; ;
                _selectedG = GRowId;
                _clists = TblTableSource.OnlyClists(AId, selectedF, selectedA, selectedG);
                _parent = parent;
            }
    
            public override nint RowsInSection(UITableView tableview, nint section)
            {
                return _clists.Count;
            }
    
            public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
            {
                Clists currentA = _clists[indexPath.Row];
    
                var cell = tableView.DequeueReusableCell(_cellID) as TblTableCell;
                if (cell == null) { cell = new TblTableCell(_cellID); }
    
                cell.UpdateCellControlsWithAuditData(currentA.Title, currentA.Name);
    
                cell.Perform.TouchUpInside += (object sender, EventArgs e) =>
                {
                    ClistDetailController view = new ClistDetailController();
                    view = _parent.Storyboard.InstantiateViewController("ClistDetailController") as ClistDetailController;
                    view.AuditorId = _Id;
                    view.SelectedFId = _selectedF;
                    view.SelectedAId = _selectedA;
                    view.SelectedGId = _selectedG;
                    view.SelectedCId = indexPath.Row;
                    _parent.NavigationController.PushViewController(view, true);
                };
                return cell;
            }
        }
    
        public class TableSource
        {
            public TableSource()
            {
            }
    
            public static List<Clists> Clists(Guid Id, int FId, int AId, int GId)
            {
                string Directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    
                ACollection _f = null;
    
                string _path = @"ACollection.xml";
                var path1 = Path.Combine(Directory, _path);
    
                XmlSerializer serializer = new XmlSerializer(typeof(ACollection));
    
                StreamReader reader = new StreamReader(path1);
                _f = (ACollection)serializer.Deserialize(reader);
                reader.Close();
    
                List<Clists> c_items = _f.Fac[FId].A[AId].Clist_Groups[GId].Clist_Items.Cast<Clists>().ToList();
                return c_items;
            }
        }
    
        public class TblTableCell : UITableViewCell
        {
            UILabel Name, lblQuestion;
            UIButton mapButton, btnPerform;
            public TblTableCell(NSString cellId) : base(UITableViewCellStyle.Default, cellId)
            {
                SelectionStyle = UITableViewCellSelectionStyle.Gray;
                ContentView.BackgroundColor = UIColor.FromRGB(0, 128, 10);
    
                var newFont = UIFont.SystemFontOfSize(12);
                Name = new UILabel();
                Name.Font = newFont;
    
                mapButton = new UIButton();
                mapButton.Font = newFont;
                mapButton.SetTitle("Map", UIControlState.Normal);
    
                btnPerform = new UIButton();
                btnPerform.Font = newFont;
                btnPerform.SetTitle("Enter", UIControlState.Normal);
    
                lblQuestion = new UILabel();
                lblQuestion.Font = newFont;
    
                ContentView.AddSubviews(new UIView[] { clauseName, btnPerformAudit, lblQuestion });
            }
    
            public UIButton MapButton
            {
                get
                {
                    return mapButton;
                }
            }
            public UIButton Perform
            {
                get
                {
                    return btnPerform;
                }
            }
            public UILabel Question
            {
                get
                {
                    return lblQuestion;
                }
            }
            public void UpdateCellControlsWithAuditData(string question, string Name)
            {
                Name.Text = _Name;
                lblQuestion.Text = question;
            }
    
            public override void LayoutSubviews()
            {
                base.LayoutSubviews();
                Name.Frame = new CGRect(10, 0, 150, 33);
                lblQuestion.Frame = new CGRect(60, 0, 700, 33);
                btnPerform.Frame = new CGRect(800, 0, 150, 33);
            }
        }
    }
    

    自动旋转的详细控制器代码:

    using Foundation;
    using System;
    using UIKit;
    using System.Xml.Serialization;
    using System.IO;
    using System.Collections.Generic;
    using System.Linq;
    using CoreGraphics;
    using System.Text;
    using System.Drawing;
    
    namespace Navigation
    {
        public partial class ClistDetailController : UIViewController
        {
            private Guid _Id;
            private int _selectedFRowId;
            private int _selectedARowId;
            private int _selectedGRowId;
            private int _selectedCRowId;
            private int _selectedCItemRowId;
            AuditsCollection _facility = null;
    
            public Guid AId
            {
                get
                {
                    return this._Id;
                }
                set
                {
                    this._Id = value;
                }
            }
    
            public int FRowId
            {
                get
                {
                    return this._FRowId;
                }
                set
                {
                    this._FRowId = value;
                }
            }
    
            public int ARowId
            {
                get
                {
                    return this._ARowId;
                }
                set
                {
                    this._ARowId = value;
                }
            }
            public int GRowId
            {
                get
                {
                    return this._GRowId;
                }
                set
                {
                    this._GRowId = value;
                }
            }
    
            public int CItemRowId
            {
                get
                {
                    return this._selectedCItemRowId;
                }
                set
                {
                    this._selectedCItemRowId = value;
                }
            }
    
            public ClistDetailController(IntPtr handle) : base(handle)
            {
            }
    
            public ClistDetailController()
            {
            }
    
            public override void ViewDidLoad()
            {
                base.ViewDidLoad();
                string Directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    
                ACollection _f = null;
    
                string _path = @"ACollection.xml";
                var path1 = Path.Combine(Directory, _path);
    
                XmlSerializer serializer = new XmlSerializer(typeof(ACollection));
    
                StreamReader reader = new StreamReader(path1);
                _f = (ACollection)serializer.Deserialize(reader);
                reader.Close();
    
                string reqStat = _f.Fac[FId].A[AId].Clist_Groups[GId].Clist_Items[CItemRowId].ReqStat.ToString();
                string Name = _f.Fac[FId].A[AId].Clist_Groups[GId].Clist_Items[CItemRowId].Name.ToString();
                string title = _f.Fac[FId].A[AId].Clist_Groups[GId].Clist_Items[CItemRowId].Title.ToString();
                string Clause = _f.Fac[FId].A[AId].Clist_Groups[GId].Clist_Items[CItemRowId].Clause.ToString();
                int Status = _f.Fac[FId].A[AId].Clist_Groups[GId].Clist_Items[CItemRowId].Status;
    
                lblClistDetail.Text = reqStat;
                lblName.Text = Name;
                lblTitle.Text = title;
                lblClause.Text = Clause;
                pvStatus.Select(Status, 0, true);
    
                txtFinding.Text = _f.Fac[FId].A[AId].Clist_Groups[GId].Clist_Items[CItemRowId].Finding.ToString();
            }
    
            partial void TouchUpInside(UIButton sender)
            {
                _f.Fac[FId].A[AId].Clist_Groups[GId].Clist_Items[CItemRowId].Finding = txtFinding.Text;
                _f.Fac[FId].A[AId].Clist_Groups[GId].Clist_Items[CItemRowId].Status = Convert.ToInt32(pvStatus.SelectedRowInComponent(0)); ;
    
                string Directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string _path = @"ACollection-" + AuditorId.ToString() + ".xml";
                var path1 = Path.Combine(Directory, _path);
    
                var c = new ACollection { AId = AId, Facility = _f.Fac };
    
                var s = new XmlSerializer(typeof(ACollection));
                var sb = new StringBuilder();
    
                using (var writer = new StringWriter(sb))
                {
                    try
                    {
                        s.Serialize(writer, c);
                        File.WriteAllText(path1, sb.ToString(), Encoding.UTF8);
                        var alert = UIAlertController.Create("Alert", "Saved successfully.", UIAlertControllerStyle.Alert);
                        alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                        UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);
                    }
                    catch (Exception e)
                    {
                        var alert = UIAlertController.Create("Alert", "Failed saving clistItem.", UIAlertControllerStyle.Alert);
                        alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                        UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);
                    }
                }
            }
        }
    
        public class StackOverflowModel : UIPickerViewModel
        {
            static string[] names = new string[] {
                "Entry1",
                "Entry2",
                "Entry3",
                "Entry4"
            };
    
            public StackOverflowModel()
            {
    
            }
    
            public override nint GetComponentCount(UIPickerView v)
            {
                return 1;
            }
    
            public override nint GetRowsInComponent(UIPickerView pickerView, nint component)
            {
                return names.Length;
            }
    
            public override string GetTitle(UIPickerView picker, nint row, nint component)
            {
    
                return names[row];
    
            }
        }
    }
    

0 个答案:

没有答案