Table 部分 URL 在 Xamarin 中调用

Table Section URL Calling in Xamarin

我想显示两个部分的tableviewcontroller。我有两个 url 可以调用以获取 json 对象并在 tableviewcontroller 中将它们制成表格。我无法弄清楚如何创建可以处理两个不同列表实例的单个 TableSource。

我在这里发布完整的源代码。如果有人能帮助我解决这个问题,我会很高兴,要么分享一个有用的 link,要么分享代码。

 public override nint RowsInSection(UITableView tableview, nint section) 

returns me an "object reference not set" error. It seems to me that once I fecth data from the first url and it tries to tabulate tableview, however, the other URL data at that time may not be ready.

namespace TPM
{
    partial class IViewController : UIViewController
    {
        public List<HumanTask> cTasks;
        public List<HumanTask> aTasks;


        public InboxViewController (IntPtr handle) : base (handle)
        {
            this.Title = "Inside";
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad ();

            GInbox ();
            CInbox ();


        }

        public void CInbox()
        {
            var client = new RestClient ("URL");
            client.Authenticator = new HttpBasicAuthenticator ("admin", "admin");
            var request = new RestRequest ("other part URL");
            request.AddHeader ("Accept", "application/json");
            request.AddHeader ("Content-Type", "application/json");

            client.ExecuteAsync (request, response => {
                cTasks = Newtonsoft.Json.JsonConvert.DeserializeObject<List<HTask>> (response.Content);

                InvokeOnMainThread (() => {

                    TableView.Source= new TableSource(cTasks,this,0);
                    TableView.ReloadData();

                });
            });


        }

        public void GInbox()
        {

            var client = new RestClient ("URL");
            client.Authenticator = new HttpBasicAuthenticator ("admin", "admin");
            var request = new RestRequest ("the rest URL");
            request.AddHeader ("Accept", "application/json");
            request.AddHeader ("Content-Type", "application/json");

            client.ExecuteAsync (request, response => {
                aTasks = Newtonsoft.Json.JsonConvert.DeserializeObject<List<HTask>> (response.Content);

                InvokeOnMainThread (() => {

                    TableView.Source= new TableSource(aTasks,this,1);
                    TableView.ReloadData();

                });
            });
        }

        public class TableSource:UITableViewSource{

            List<HTask>cTableItems;
            List<HTask>aTableItems;
            int defi;
            string cellIdentifier="TableCell";
            private IViewController iv;

            public TableSource (List<HTask>items, IViewController vc, int def)
            {
                if(def==0)
                {
                    cTableItems=items;
                }
                else if(def==1)
                {
                    aTableItems=items;
                }
                iv=vc;
                defi=def;
            }


            public override nint NumberOfSections (UITableView tableView)
            {
                return 2;
            }

            public override nfloat GetHeightForHeader (UITableView tableView, nint section)
            {
                if(section==0){
                    return 40;
                }
                if(section == 1) {
                    return 40;
                }

                return 40;

            }

            public override UIView GetViewForHeader(UITableView tableView, nint section)
            {
                UIView headerView = new UIView(new RectangleF (0, 0, (float)UIScreen.MainScreen.Bounds.Width, (float)tableView.SectionHeaderHeight));
                   headerView.BackgroundColor = UIColor.Black;
                    UILabel sectionTitle = new UILabel( new RectangleF(10, (float)((headerView.Frame.Height - 22) / 2), 200, 24));
                    sectionTitle.Font = UIFont.BoldSystemFontOfSize(22);
                    sectionTitle.TextColor = UIColor.White;
                    sectionTitle.TextAlignment = UITextAlignment.Right;
                    if (section == 0) {
                      sectionTitle.Text = "Cmed";
                     }
                    else if (section == 1) {
                      sectionTitle.Text = "Asy";
                    }
                    headerView.AddSubview(sectionTitle);

                    return headerView;

            }


            public override nint RowsInSection(UITableView tableview, nint section)
            {
                if (section == 0)
                    return cTableItems.Count;
                else 
                    return aTableItems.Count;
            }

            public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
            {
                UITableViewCell cell = tableView.DequeueReusableCell(cellIdentifier);
                if (cell == null)
                    cell = new UITableViewCell(UITableViewCellStyle.Subtitle, cellIdentifier);
                if (indexPath.Section == 0) {
                    cell.TextLabel.Text = cTableItems [indexPath.Row].displayName;
                    cell.DetailTextLabel.Lines = 3;
                    cell.DetailTextLabel.Text = "Process ID:" + cTableItems [indexPath.Row].processInstanceId + "\n" + DateTime.Parse (Convert.ToDateTime (cTableItems [indexPath.Row].createdOn).ToShortTimeString ());
                    if (cTableItems [indexPath.Row].priority == 0) {
                        cell.ImageView.Image = UIImage.FromFile ("Images/green.png");
                    }
                    else if (cTableItems [indexPath.Row].priority == 1) {
                        cell.ImageView.Image = UIImage.FromFile ("Images/yellow.png");
                    }
                    else if (cTableItems [indexPath.Row].priority == 2) {
                        cell.ImageView.Image = UIImage.FromFile ("Images/red.png");
                    }

                }
                else if (indexPath.Section == 1) {
                    cell.TextLabel.Text = assignTableItems [indexPath.Row].displayName;
                    cell.DetailTextLabel.Lines = 3;
                    cell.DetailTextLabel.Text = "Process ID:" + aTableItems [indexPath.Row].processInstanceId + "\n" + DateTime.Parse (Convert.ToDateTime (aTableItems [indexPath.Row].createdOn).ToShortTimeString ());

                    if (aTableItems [indexPath.Row].priority == 0) {
                        cell.ImageView.Image = UIImage.FromFile ("Images/green.png");
                    }
                    else if (aTableItems [indexPath.Row].priority == 1) {
                        cell.ImageView.Image = UIImage.FromFile ("Images/yellow.png");
                    }
                    else if (aTableItems [indexPath.Row].priority == 2) {
                        cell.ImageView.Image = UIImage.FromFile ("Images/red.png");
                    }
                }

                cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;

                return cell;
            }

            public override nfloat GetHeightForRow (UITableView tableView, Foundation.NSIndexPath indexPath)
            {
                return 60;
            }


        }
    }
}

基于 Jason 的回答:我收到以下错误:

基于 Jason 回答的第二次更新:

基于 Jason 回答的第三次更新:

不是在构造函数中传递两组数据,而是将它们作为属性传递 - 这允许您在创建 Source 之后设置它们,这很有用,因为您正在异步获取数据。

public class TableSource:UITableViewSource{

            public List<HTask> cTableItems;
            public List<HTask> aTableItems;


            string cellIdentifier="TableCell";
            private IViewController iv;

            public TableSource (IViewController vc)
            {
              aTableItems = new List<HTask>();
              cTableItems = new List<HTask>();
              iv=vc;
            }

然后在创建VC

创建一次
    public override void ViewDidLoad()
    {
        base.ViewDidLoad ();

        TableView.Source = new TableSource(this);

        GInbox ();
        CInbox ();


    }

最后,当您获得数据时,不要重新创建您的源,只需使用数据更新它:(对您的其他数据集重复相同的操作)

    public void CInbox()
    {
        var client = new RestClient ("URL");
        client.Authenticator = new HttpBasicAuthenticator ("admin", "admin");
        var request = new RestRequest ("other part URL");
        request.AddHeader ("Accept", "application/json");
        request.AddHeader ("Content-Type", "application/json");
        //request.Method = (string)"GET";

        client.ExecuteAsync (request, response => {

            cTasks = Newtonsoft.Json.JsonConvert.DeserializeObject<List<HTask>> (response.Content);

            InvokeOnMainThread (() => {

                ((TableSource)this.TableView.Source).cTableItems = cTasks;
                TableView.ReloadData();

            });
        });