0

I'm doing an app with Xamarin iOS. I put a UITableView on XCode, so that when I click on a button, it retrieves from the database and slot it in. I'm able to put it onto a row, but couldn't figure it out how to have multiple rows of data in it. This is my partial code from which I'm able to display a row of data.

cmd.CommandType = CommandType.Text;
dr = cmd.ExecuteReader();
while (dr.Read())
    {
        var table = new UITableView(this.retrieveData.Frame);
        string[] tableItems = new String[] {dr["admin_num"] + ", " + dr["name"]};
        table.Source = new TableSource(tableItems);
        Add (table);
    }

2 Answers 2

1

You are creating a completely new TableView for each row in your data. Instead, you should loop through your data and create a data structure (List, array, etc) containing ALL of the data you want to display, and then pass that data to your TableView/Source.

  cmd.CommandType = CommandType.Text;
  dr = cmd.ExecuteReader();

  // you will need a class mydata with Num and Name properties
  List<mydata> data = new List<mydata>();

  while (dr.Read())
  {
    data.Add(new mydata { Num = dr["admin_num"], Name = dr["name"] });
  }

  dr.Close();

  var table = new UITableView(this.retrieveData.Frame);
  table.Source = new TableSource(data);
  Add (table);
Sign up to request clarification or add additional context in comments.

8 Comments

thanks for the advice, may I know what is the num and name properties for ?
Based on your code, I'm assuming those are the data elements you want to display in your table. That class should reflect whatever your data model is.
is there any example on it where I can refer to ?
but my data comes from database SQLite.
|
0

What you need to do is this:

 public List<DemoClass> getDemoClassList()
    {
        List<DemoClass> lstDemoClass;
        DemoClass objDemoClass;
        try
        {
            String strCommandText;

            strCommandText = "SELECT * FROM DemoClass ";

            command = new SqliteCommand(strCommandText, connection);
            lstDemoClass = new List<DemoClass>();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    objDemoClass = new Homes(false);
                    objDemoClass.ID = Convert.ToInt32(reader[0]);
                    objDemoClass.Name = Convert.ToString(reader[1]);
                    lstDemoClass.Add(objDemoClass);

                }
            }
            return lstDemoClass;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            command.Dispose();
            command = null;
            lstDemoClass = null;
            objDemoClass = null;
        }
    }
             public void BindList()
             {   
                List<DemoClass> lstDemoClass = new List<DemoClass>();
                DemoClass hm = new DemoClass();
                lstDemoClass = (List<DemoClass>)hm.getDemoClassList();

                TableViewDataSource tdatasource = new TableViewDataSource(this, lstDemoClass);
                table.Hidden = false;
                table.DataSource = tdatasource;
                table.Delegate = new TableViewDelegate(this, table, lstDemoClass);
                table.ReloadData();
             }

The getDemoClassList() will give the retrieved list from SQLite table, and later you can bind the list to the table datasource.

UPDATE: As per your request I have updated my comment with the code for datasource and its delegate classes. Now in this same class you need to add the following subclasses:

#region TableDelegate
    public class TableViewDelegate : UITableViewDelegate
    {
        private DemoPageViewController _Controller;
        private List<DemoClass> lst;

        public TableViewDelegate(DemoPageViewController controller ,UITableView tableView, List<DemoClass> tblList)
        {
            try 
            {
                this._Controller = controller;
                this.lst = tblList;
            }
            catch(Exception ex)
            {

            }
        }

        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            try 
            {
                //This loads the activity spinner till the selection code is completed
                _Controller._loadPop = new LoadingOverlay (new System.Drawing.RectangleF(0,0,_Controller.View.Frame.Width,_Controller.View.Frame.Height),"Loading...");
                _Controller.View.Add ( _Controller._loadPop );

                // spin up a new thread to do some long running work using StartNew
                Task.Factory.StartNew (
                    // tasks allow you to use the lambda syntax to pass work
                    () => {
                    InvokeOnMainThread(delegate{

                        DemoClass f = lst[indexPath.Row];

                        //Add your code here, usually some navigation or showing a popup
                    });
                }).ContinueWith(t => InvokeOnMainThread(() => {
                    //Hide the activity spinner
                    _Controller._loadPop.Hide();
                }));

            }
            catch(Exception ex)
            {

            }
            finally     
            {
            }
        }
    }

    #endregion 

    #region TableDataSource

    private class TableViewDataSource : UITableViewDataSource
    {
        static NSString kCellIdentifier = new NSString("MyIdentifier");
        private List<DemoClass> lst;
        private DemoPageViewController controller;

        public TableViewDataSource (DemoPageViewController controller ,List<DemoClass> tblLst)
        {
            this.controller = controller;
            this.lst = tblLst;
        }

        public override int NumberOfSections (UITableView tableView)
        {
            return 1;
        }

        public override int RowsInSection (UITableView tableView, int section)
        {
            return lst.Count;
        }

        // Override to support conditional editing of the table view.
        public override bool CanEditRow (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
        {
            // Return false if you do not want the specified item to be editable.
            return false;
        }


        public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
        {
            try 
            {
                UITableViewCell cell = tableView.DequeueReusableCell (kCellIdentifier);
                if (cell == null)
                {
                    cell = new UITableViewCell (UITableViewCellStyle.Subtitle, kCellIdentifier);
                    cell.Tag = Environment.TickCount;
                }

                DemoClass objDemo = lst[indexPath.Row];
                cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;

                cell.ImageView.Image = UIImage.FromFile("Images/CameraImg.png");
                cell.DetailTextLabel.Text = "Show some detail: " + objDemo.DemoDescription.ToString();
                cell.TextLabel.Text = "Some Title: " + objDemo.DemoTitle.ToString();
                return cell;
            }
            catch(Exception ex)
            {
                return null;
            }
            finally     
            {

            }
        }
    }

    #endregion 

Hope it helps.

1 Comment

will appreciate it if you can post it. =))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.