Xamarin 中的表格部分 URL 调用 [英] Table Section URL Calling in Xamarin

查看:31
本文介绍了Xamarin 中的表格部分 URL 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我在这里发布完整的源代码.如果有人能够帮助我解决这个问题,我会很高兴,或者分享一个有用的链接,或者分享代码.

<块引用>

 public override nint RowsInSection(UITableView tableview, nint section)

返回未设置对象引用"错误.在我看来,一旦我从第一个 url 获取数据并尝试将 tableview 制表,但是,当时的其他 URL 数据可能还没有准备好.

命名空间 TPM{部分类 IViewController : UIViewController{公共列表任务;公共列表任务;public InboxViewController (IntPtr handle) : base (handle){this.Title = "里面";}公共覆盖无效 ViewDidLoad(){base.ViewDidLoad();收件箱();收件箱();}公共无效 CInbox(){var client = new RestClient("URL");client.Authenticator = new HttpBasicAuthenticator ("admin", "admin");var request = new RestRequest("其他部分网址");request.AddHeader("Accept", "application/json");request.AddHeader("Content-Type", "application/json");client.ExecuteAsync(请求,响应 => {cTasks = Newtonsoft.Json.JsonConvert.DeserializeObject>(响应.内容);InvokeOnMainThread (() => {TableView.Source= new TableSource(cTasks,this,0);TableView.ReloadData();});});}公共无效 GInbox(){var client = new RestClient("URL");client.Authenticator = new HttpBasicAuthenticator ("admin", "admin");var request = new RestRequest("其余 URL");request.AddHeader("Accept", "application/json");request.AddHeader("Content-Type", "application/json");client.ExecuteAsync(请求,响应 => {aTasks = Newtonsoft.Json.JsonConvert.DeserializeObject>(响应.内容);InvokeOnMainThread (() => {TableView.Source= new TableSource(aTasks,this,1);TableView.ReloadData();});});}公共类表源:UITableViewSource{ListcTableItems;ListaTableItems;国际定义;字符串 cellIdentifier="TableCell";私有 IViewController iv;public TableSource (Listitems, IViewController vc, int def){如果(定义== 0){cTableItems=items;}否则如果(定义== 1){aTableItems=items;}iv=vc;定义=定义;}公共覆盖 nint NumberOfSections (UITableView tableView){返回2;}公共覆盖 nfloat GetHeightForHeader (UITableView tableView, nint 部分){如果(部分== 0){返回 40;}如果(部分== 1){返回 40;}返回 40;}公共覆盖 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;如果(部分== 0){sectionTitle.Text = "Cmed";}否则如果(部分== 1){sectionTitle.Text = "Asy";}headerView.AddSubview(sectionTitle);返回标题视图;}公共覆盖 nint RowsInSection(UITableView tableview, nint section){如果(部分== 0)返回 cTableItems.Count;别的返回 aTableItems.Count;}公共覆盖 UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath){UITableViewCell cell = tableView.DequeueReusableCell(cellIdentifier);如果(单元格 == 空)cell = new UITableViewCell(UITableViewCellStyle.Subtitle, cellIdentifier);如果(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");}}否则如果(indexPath.Section == 1){cell.TextLabel.Text = assignTableItems [indexPath.Row].displayName;cell.DetailTextLabel.Lines = 3;cell.DetailTextLabel.Text = "进程 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;返回单元格;}公共覆盖 nfloat GetHeightForRow (UITableView tableView, Foundation.NSIndexPath indexPath){返回 60;}}}}

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

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

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

解决方案

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

公共类TableSource:UITableViewSource{公共列表cTableItems;公共列表aTableItems;字符串 cellIdentifier="TableCell";私有 IViewController iv;公共表源(IViewController vc){aTableItems = new List();cTableItems = new List();iv=vc;}

然后在创建 VC 时创建一次

 public override void ViewDidLoad(){base.ViewDidLoad();TableView.Source = new TableSource(this);收件箱();收件箱();}

最后,当你得到数据时不要重新创建你的源,只需用数据更新它:(对你的其他数据集重复同样的事情)

 public void CInbox(){var client = new RestClient("URL");client.Authenticator = new HttpBasicAuthenticator ("admin", "admin");var request = new RestRequest("其他部分网址");request.AddHeader("Accept", "application/json");request.AddHeader("Content-Type", "application/json");//request.Method = (string)"GET";client.ExecuteAsync(请求,响应 => {cTasks = Newtonsoft.Json.JsonConvert.DeserializeObject>(响应.内容);InvokeOnMainThread (() => {((TableSource)this.TableView.Source).cTableItems = cTasks;TableView.ReloadData();});});

I would like to display two section tableviewcontroller. I have two urls to call to get json object and tabulate them in tableviewcontroller. I could not able to figure out how to Create a single TableSource that can handle two different instances of List.

I am posting here full source code here. I will be glad if any one able to help me out in this problem, either share a link that would be useful, or share the code.

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


        }
    }
}

Based on Jason answer: I am getting the following error:

Second update based on Jason answer:

Third update based on Jason answer:

解决方案

Instead of passing the two sets of data in the constructor, pass them as properties - this allows you to set them after the Source has been created, which is useful since you are getting the data asynchronously.

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

Then create your Source once when you create the VC

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

        TableView.Source = new TableSource(this);

        GInbox ();
        CInbox ();


    }

Finally, when you get the data don't recreate your Source, just update it with the data: (repeat the same thing for your other dataset)

    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();

            });
        });

这篇关于Xamarin 中的表格部分 URL 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆